|
| 1 | +#--------------------------------------------------------------------------------- |
| 2 | +.SUFFIXES: |
| 3 | +#--------------------------------------------------------------------------------- |
| 4 | + |
| 5 | +ifeq ($(strip $(DEVKITPRO)),) |
| 6 | +$(error "Please set DEVKITPRO in your environment. export DEVKITPRO=<path to>/devkitpro") |
| 7 | +endif |
| 8 | + |
| 9 | +TOPDIR ?= $(CURDIR) |
| 10 | +include $(DEVKITPRO)/libnx/switch_rules |
| 11 | + |
| 12 | + |
| 13 | +#--------------------------------------------------------------------------------- |
| 14 | +# options for code generation |
| 15 | +#--------------------------------------------------------------------------------- |
| 16 | +ARCH := -march=armv8-a -mtune=cortex-a57 -mtp=soft -fPIE |
| 17 | + |
| 18 | +CFLAGS := -g -Wall -O2 -ffunction-sections \ |
| 19 | + $(ARCH) $(DEFINES) |
| 20 | + |
| 21 | +CXXFLAGS := $(CFLAGS) -fno-rtti -fno-exceptions -std=gnu++11 |
| 22 | + |
| 23 | +ASFLAGS := -g $(ARCH) |
| 24 | +LDFLAGS = -specs=$(DEVKITPRO)/libnx/switch.specs -g $(ARCH) -Wl,-Map,$(notdir $*.map) |
| 25 | + |
| 26 | +LIBS := -lnx |
| 27 | + |
| 28 | +#--------------------------------------------------------------------------------- |
| 29 | +# list of directories containing libraries, this must be the top level containing |
| 30 | +# include and lib |
| 31 | +#--------------------------------------------------------------------------------- |
| 32 | + |
| 33 | +LIBDIRS := $(PORTLIBS) $(LIBNX) |
| 34 | +export INCLUDE := $(foreach dir,$(LIBDIRS),-I$(dir)/include) \ |
| 35 | + -I$(CURDIR)/$(BUILD) |
| 36 | +CFLAGS += $(INCLUDE) -DSWITCH |
| 37 | + |
| 38 | + |
| 39 | +ifndef PYVERS |
| 40 | + PYVERS := 2.7.12 |
| 41 | +endif |
| 42 | + |
| 43 | +ANAME := libpython$(shell echo $(PYVERS) | sed 's/\([0-9]*\.\([0-9]*\)\).*/\1/').a |
| 44 | + |
| 45 | +OUTDIR := nxpy$(PYVERS) |
| 46 | + |
| 47 | +PYDIR := Python-$(PYVERS) |
| 48 | +PYLINK := https://www.python.org/ftp/python/$(PYVERS)/Python-$(PYVERS).tgz |
| 49 | +PYFILE := py.tgz |
| 50 | + |
| 51 | +.PHONY: all clean |
| 52 | + |
| 53 | + |
| 54 | +all: distfPY |
| 55 | + |
| 56 | +distfPY: linkPY |
| 57 | + @[ -d "$(OUTDIR)" ] || mkdir -p $(OUTDIR) |
| 58 | + @[ -d "$(OUTDIR)/include" ] || mkdir -p $(OUTDIR)/include |
| 59 | + @[ -d "$(OUTDIR)/include/nxpy" ] || mkdir -p $(OUTDIR)/include/nxpy |
| 60 | + @[ -d "$(OUTDIR)/lib" ] || mkdir -p $(OUTDIR)/lib |
| 61 | + cp $(ANAME) $(OUTDIR)/lib/ |
| 62 | + cp $(PYDIR)/Include/* $(OUTDIR)/include/nxpy |
| 63 | + cp $(PYDIR)/pyconfig.h $(OUTDIR)/include/nxpy/ |
| 64 | + cp $(PYDIR)/Lib/socket.py $(PYDIR)/Lib/socket.pyX |
| 65 | + cat $(PYDIR)/Lib/socket.pyX | sed 's/'"'"'getpeername'"'"', //g' >$(PYDIR)/Lib/socket.py |
| 66 | + rm $(PYDIR)/Lib/socket.pyX |
| 67 | + cd $(PYDIR)/Lib && ls *.py -1 | xargs zip ../../$(OUTDIR)/python.zip && find json/ encodings/ -type f | xargs zip ../../$(OUTDIR)/python.zip |
| 68 | + |
| 69 | + touch distfPY |
| 70 | + |
| 71 | +linkPY: soospatchPY |
| 72 | + cd $(PYDIR) && make LIBRARY="$(ANAME)" LDLIBRARY="$(ANAME)" $(ANAME) && cd .. && cp $(PYDIR)/$(ANAME) . && touch linkPY |
| 73 | + |
| 74 | +soospatchPY: compilePY |
| 75 | + cp $(PYDIR)/pyconfig.h $(PYDIR)/pyconfig.h_old |
| 76 | + cat $(PYDIR)/pyconfig.h_old | sed 's/^\([^#][^#]*#undef PY_FORMAT_LONG_LONG.*\)/\#define PY_FORMAT_LONG_LONG \"ll\" \1/' | sed 's/^\(#define socklen_t int\)/#undef socklen_t/' | sed 's/^\(#define socklen_t int\)/#undef socklen_t/' | sed 's/^\(struct servent {char*s_name;char**s_aliases;int s_port;char*s_proto;};\)/\/\/\1/' | sed 's/^\(struct protoent{char*p_name;char**p_aliases;int p_proto;};\)/\/\/\1/' | sed 's/^\([^#][^#]*#undef HAVE_FSTATVFS.*\)/\#undef HAVE_FSTATVFS \1/' | sed 's/#define HAVE_\(LSTAT\|POLL\|UNAME\|WAIT3\|WAIT4\|WAITPID\|ALARM\|DLFCN_H\|DLOPEN\|DYNAMIC_LOADING\|GETC_UNLOCKED\|GETENTROPY\|GETITIMER\|GETPWENT\|MMAP\|OPENPTY\|PAUSE\|READLINK\|SETITIMER\|SIGACTION\|SIGINTERRUPT\|TERMIOS_H\|EXECV\|FORK\|GETEGID\|GETEUID\|GETGID\|GETPPID\|GETUID\|KILL\|PIPE\|POPEN\|SYSTEM\|TTYNAME\|SYMLINK\|UTIME_H\|FDATASYNC\|TZNAME\|DECL_TZNAME\|WORKING_TZSET\).*/#undef HAVE_\1/g' | sed 's/^#define HAVE_\(STATVFS\|SYS_STATVFS_H\|FDATASYNC\|SYMLINK\|EXECV\|FORK\|GETEGID\|GETEUID\|GETGID\|GETPPID\|GETUID\|KILL\|PIPE\|POPEN\|SYSTEM\|TTYNAME\|SYMLINK\|UTIME_H\|FDATASYNC\|TZNAME\|DECL_TZNAME\|WORKING_TZSET\).*/#undef HAVE_\1/' | sed 's/^\([^#][^#]*#undef HAVE_SELECT.*\)/\#define HAVE_SELECT \1/' | sed 's/^\([^#][^#]*#undef HAVE_GETADDRINFO.*\)/struct servent {char*s_name;char**s_aliases;int s_port;char*s_proto;};\nstruct protoent{char*p_name;char**p_aliases;int p_proto;};\n#define SOCK_RAW 3\n#define SOCK_SEQPACKET 5\n#define IN_CLASSA_NSHIFT 24 \1/g' >$(PYDIR)/pyconfig.h |
| 77 | + cp $(PYDIR)/Modules/posixmodule.c $(PYDIR)/Modules/posixmodule.c_old |
| 78 | + cat $(PYDIR)/Modules/posixmodule.c_old | sed 's/\(^[^rt]*time_t atime, mtime;.*\)/return NULL; \1/' | sed 's/\(^[^ri]*i = (int)umask(i);.*\)/return NULL; \1/' | sed 's/^\([^#][^#]*#undef HAVE_FSTATVFS.*\)/\#undef HAVE_FSTATVFS \1/' | sed 's/#define HAVE_\(EXECV\|FORK\|GETEGID\|GETEUID\|GETGID\|GETPPID\|GETUID\|KILL\|PIPE\|POPEN\|SYSTEM\|TTYNAME\|SYMLINK\|UTIME_H\|FDATASYNC\).*/#undef HAVE_\1/g' | sed 's/^#define HAVE_\(STATVFS\|SYS_STATVFS_H\|FDATASYNC\|FTIME\|SYMLINK\|EXECV\|FORK\|GETEGID\|GETEUID\|GETGID\|GETPPID\|GETUID\|KILL\|PIPE\|POPEN\|SYSTEM\|TTYNAME\|SYMLINK\|UTIME_H\|FDATASYNC\).*/#undef HAVE_\1/' >$(PYDIR)/Modules/posixmodule.c |
| 79 | + cp $(PYDIR)/Modules/socketmodule.c $(PYDIR)/Modules/socketmodule.c_old |
| 80 | + cat $(PYDIR)/Modules/socketmodule.c_old | sed 's/ sizeof(addr->sa_data)/ 28/g' >$(PYDIR)/Modules/socketmodule.c |
| 81 | + #cp $(PYDIR)/Objects/exceptions.c $(PYDIR)/Objects/exceptions.c_old |
| 82 | + #cat $(PYDIR)/Objects/exceptions.c_old | sed 's/ESHUTDOWN/110/g' >$(PYDIR)/Objects/exceptions.c |
| 83 | + #cp $(PYDIR)/Python/pytime.c $(PYDIR)/Python/pytime.c_old |
| 84 | + #cat $(PYDIR)/Python/pytime.c_old | sed 's/CLOCK_MONOTONIC/(clockid_t)4/g' >$(PYDIR)/Python/pytime.c |
| 85 | + cp $(PYDIR)/Makefile $(PYDIR)/Makefile_old |
| 86 | + cat $(PYDIR)/Makefile_old | sed 's/^\(Python\/\$(DYNLOADFILE) \\\)/#Python\/\$(DYNLOADFILE) \\/' >$(PYDIR)/Makefile |
| 87 | + touch soospatchPY |
| 88 | + |
| 89 | +compilePY: extractedPY patchPY |
| 90 | + cd $(PYDIR) && ./configure CC="$(CC)" CXX="$(CXX)" AS="$(AS)" AR="$(AR)" OBJCOPY="$(OBJCOPY)" STRIP="$(STRIP)" NM="$(NM)" RANLIB="$(RANLIB)" CFLAGS="$(CFLAGS)" CXXFLAGS="$(CXXFLAGS)" ASFLAGS="$(ASFLAGS)" LDFLAGS="$(LDFLAGS)" CONFIG_SITE="config.site" --disable-shared --without-threads --without-doc-strings --disable-ipv6 --host=aarch64-none-elf --build=`./config.guess` && cd .. && touch compilePY |
| 91 | + |
| 92 | +patchPY: |
| 93 | + cp $(PYDIR)/configure $(PYDIR)/configure_old |
| 94 | + cat $(PYDIR)/configure_old | sed 's/ \*\-\*\-linux\*)/ \*\-\*\-linux\*\|aarch64\-none\-elf)/g' >$(PYDIR)/configure |
| 95 | + echo ac_cv_file__dev_ptmx=no >$(PYDIR)/config.site |
| 96 | + echo ac_cv_file__dev_ptc=no >>$(PYDIR)/config.site |
| 97 | + echo ac_cv_lib_dl_dlopen=no >>$(PYDIR)/config.site |
| 98 | + cp $(PYDIR)/Modules/Setup.dist $(PYDIR)/Modules/Setup.dist_old |
| 99 | + cat $(PYDIR)/Modules/Setup.dist_old | sed 's/^\([^#].* pwdmodule\.c.*\)/#\1/' | sed 's/^#\(array\|cmath\|math\|_struct\|operator\|_random\|_collections\|itertools\|strop\|unicodedata\|_io\|_csv\|_md5\|_sha\|_sha256\|_sha512\|binascii\|zlib\|select\|cStringIO\|time\|_functools\|_socket\|datetime\|_bisect\)\(.*\)/\1\2/' | sed "s#\\(zlib[^\$$]*\\)\$$(prefix)\\([^\$$]*\\)\$$(exec_prefix)\\(.*\\)#\1$(DEVKITPRO)/portlibs/armv8-a\2$(DEVKITPRO)/portlibs/armv8-a\3#" >$(PYDIR)/Modules/Setup.dist |
| 100 | + |
| 101 | + touch patchPY |
| 102 | + |
| 103 | +extractedPY: $(PYFILE) |
| 104 | + tar xfzv $(PYFILE) && touch extractedPY |
| 105 | + |
| 106 | +$(PYFILE): |
| 107 | + wget -O "$(PYFILE)" "$(PYLINK)" || curl -Lo "$(PYFILE)" "$(PYLINK)" |
| 108 | + |
| 109 | +clean: |
| 110 | + @rm -rf $(PYDIR) $(PYFILE) patchPY extractedPY compilePY linkPY distfPY soospatchPY libpython*.a |
| 111 | + |
| 112 | + |
| 113 | +#--------------------------------------------------------------------------------- |
| 114 | +# TARGET is the name of the output |
| 115 | +# BUILD is the directory where object files & intermediate files will be placed |
| 116 | +# SOURCES is a list of directories containing source code |
| 117 | +# DATA is a list of directories containing data files |
| 118 | +# INCLUDES is a list of directories containing header files |
| 119 | +# EXEFS_SRC is the optional input directory containing data copied into exefs, if anything this normally should only contain "main.npdm". |
| 120 | +# |
| 121 | +# NO_ICON: if set to anything, do not use icon. |
| 122 | +# NO_NACP: if set to anything, no .nacp file is generated. |
| 123 | +# APP_TITLE is the name of the app stored in the .nacp file (Optional) |
| 124 | +# APP_AUTHOR is the author of the app stored in the .nacp file (Optional) |
| 125 | +# APP_VERSION is the version of the app stored in the .nacp file (Optional) |
| 126 | +# APP_TITLEID is the titleID of the app stored in the .nacp file (Optional) |
| 127 | +# ICON is the filename of the icon (.jpg), relative to the project folder. |
| 128 | +# If not set, it attempts to use one of the following (in this order): |
| 129 | +# - <Project name>.jpg |
| 130 | +# - icon.jpg |
| 131 | +# - <libnx folder>/default_icon.jpg |
| 132 | +#--------------------------------------------------------------------------------- |
| 133 | +TARGET := $(notdir $(CURDIR)) |
| 134 | +BUILD := build |
| 135 | +SOURCES := source |
| 136 | +DATA := data |
| 137 | +INCLUDES := include |
| 138 | +EXEFS_SRC := exefs_src |
| 139 | +APP_TITLEID := Pynx |
| 140 | +APP_AUTHOR := nx-python Authors, Python Software Foundation |
| 141 | +APP_VERSION := 0.1.0-alpha |
| 142 | + |
| 143 | +#--------------------------------------------------------------------------------- |
| 144 | +# no real need to edit anything past this point unless you need to add additional |
| 145 | +# rules for different file extensions |
| 146 | +#--------------------------------------------------------------------------------- |
| 147 | +ifneq ($(BUILD),$(notdir $(CURDIR))) |
| 148 | +#--------------------------------------------------------------------------------- |
| 149 | + |
| 150 | +export OUTPUT := $(CURDIR)/$(TARGET) |
| 151 | +export TOPDIR := $(CURDIR) |
| 152 | + |
| 153 | +export VPATH := $(foreach dir,$(SOURCES),$(CURDIR)/$(dir)) \ |
| 154 | + $(foreach dir,$(DATA),$(CURDIR)/$(dir)) |
| 155 | + |
| 156 | +export DEPSDIR := $(CURDIR)/$(BUILD) |
| 157 | + |
| 158 | +CFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.c))) |
| 159 | +CPPFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.cpp))) |
| 160 | +SFILES := $(foreach dir,$(SOURCES),$(notdir $(wildcard $(dir)/*.s))) |
| 161 | +BINFILES := $(foreach dir,$(DATA),$(notdir $(wildcard $(dir)/*.*))) |
| 162 | + |
| 163 | +#--------------------------------------------------------------------------------- |
| 164 | +# use CXX for linking C++ projects, CC for standard C |
| 165 | +#--------------------------------------------------------------------------------- |
| 166 | +ifeq ($(strip $(CPPFILES)),) |
| 167 | +#--------------------------------------------------------------------------------- |
| 168 | + export LD := $(CC) |
| 169 | +#--------------------------------------------------------------------------------- |
| 170 | +else |
| 171 | +#--------------------------------------------------------------------------------- |
| 172 | + export LD := $(CXX) |
| 173 | +#--------------------------------------------------------------------------------- |
| 174 | +endif |
| 175 | +#--------------------------------------------------------------------------------- |
| 176 | + |
| 177 | +export OFILES := $(addsuffix .o,$(BINFILES)) \ |
| 178 | + $(CPPFILES:.cpp=.o) $(CFILES:.c=.o) $(SFILES:.s=.o) |
| 179 | + |
| 180 | +export INCLUDE := $(foreach dir,$(INCLUDES),-I$(CURDIR)/$(dir)) \ |
| 181 | + $(foreach dir,$(LIBDIRS),-I$(dir)/include) \ |
| 182 | + -I$(CURDIR)/$(BUILD) |
| 183 | + |
| 184 | +export LIBPATHS := $(foreach dir,$(LIBDIRS),-L$(dir)/lib) |
| 185 | + |
| 186 | +export BUILD_EXEFS_SRC := $(TOPDIR)/$(EXEFS_SRC) |
| 187 | + |
| 188 | +ifeq ($(strip $(ICON)),) |
| 189 | + icons := $(wildcard *.jpg) |
| 190 | + ifneq (,$(findstring $(TARGET).jpg,$(icons))) |
| 191 | + export APP_ICON := $(TOPDIR)/$(TARGET).jpg |
| 192 | + else |
| 193 | + ifneq (,$(findstring icon.jpg,$(icons))) |
| 194 | + export APP_ICON := $(TOPDIR)/icon.jpg |
| 195 | + endif |
| 196 | + endif |
| 197 | +else |
| 198 | + export APP_ICON := $(TOPDIR)/$(ICON) |
| 199 | +endif |
| 200 | + |
| 201 | +ifeq ($(strip $(NO_ICON)),) |
| 202 | + export NROFLAGS += --icon=$(APP_ICON) |
| 203 | +endif |
| 204 | + |
| 205 | +ifeq ($(strip $(NO_NACP)),) |
| 206 | + export NROFLAGS += --nacp=$(CURDIR)/$(TARGET).nacp |
| 207 | +endif |
| 208 | + |
| 209 | +ifneq ($(APP_TITLEID),) |
| 210 | + export NACPFLAGS += --titleid=$(APP_TITLEID) |
| 211 | +endif |
| 212 | + |
| 213 | +.PHONY: $(BUILD) clean all |
| 214 | + |
| 215 | +#--------------------------------------------------------------------------------- |
| 216 | +all: $(BUILD) |
| 217 | + |
| 218 | +$(BUILD): |
| 219 | + @[ -d $@ ] || mkdir -p $@ |
| 220 | + @$(MAKE) --no-print-directory -C $(BUILD) -f $(CURDIR)/Makefile |
| 221 | + |
| 222 | +#--------------------------------------------------------------------------------- |
| 223 | +clean: |
| 224 | + @echo clean ... |
| 225 | + @rm -fr $(BUILD) $(TARGET).pfs0 $(TARGET).nso $(TARGET).nro $(TARGET).nacp $(TARGET).elf |
| 226 | + |
| 227 | + |
| 228 | +#--------------------------------------------------------------------------------- |
| 229 | +else |
| 230 | +.PHONY: all |
| 231 | + |
| 232 | +DEPENDS := $(OFILES:.o=.d) |
| 233 | + |
| 234 | +#--------------------------------------------------------------------------------- |
| 235 | +# main targets |
| 236 | +#--------------------------------------------------------------------------------- |
| 237 | +all : $(OUTPUT).pfs0 $(OUTPUT).nro |
| 238 | + |
| 239 | +$(OUTPUT).pfs0 : $(OUTPUT).nso |
| 240 | + |
| 241 | +$(OUTPUT).nso : $(OUTPUT).elf |
| 242 | + |
| 243 | +ifeq ($(strip $(NO_NACP)),) |
| 244 | +$(OUTPUT).nro : $(OUTPUT).elf $(OUTPUT).nacp |
| 245 | +else |
| 246 | +$(OUTPUT).nro : $(OUTPUT).elf |
| 247 | +endif |
| 248 | + |
| 249 | +$(OUTPUT).elf : $(OFILES) |
| 250 | + |
| 251 | +#--------------------------------------------------------------------------------- |
| 252 | +# you need a rule like this for each extension you use as binary data |
| 253 | +#--------------------------------------------------------------------------------- |
| 254 | +%.bin.o : %.bin |
| 255 | +#--------------------------------------------------------------------------------- |
| 256 | + @echo $(notdir $<) |
| 257 | + @$(bin2o) |
| 258 | + |
| 259 | +-include $(DEPENDS) |
| 260 | + |
| 261 | +#--------------------------------------------------------------------------------------- |
| 262 | +endif |
| 263 | +#--------------------------------------------------------------------------------------- |
0 commit comments