-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Nintendo switch support #8069
Nintendo switch support #8069
Changes from 13 commits
4874be9
c40ecec
8a0bff9
c349083
c8e0c5a
e4dff3a
14ebe3e
ed18079
0cf2e68
1935c16
f7ec343
3afe344
9b300a5
d3cf79e
de8eb3d
06ba653
cd42369
c137097
3561f83
25ae23a
b525bcc
66c0ed9
bec2c96
b233ce0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,6 +83,31 @@ compiler gcc: | |
props: {hasSwitchRange, hasComputedGoto, hasCpp, hasGcGuard, hasGnuAsm, | ||
hasAttribute}) | ||
|
||
# GNU C and C++ Compiler | ||
compiler nintendoSwitchGCC: | ||
result = ( | ||
name: "switch_gcc", | ||
objExt: "o", | ||
optSpeed: " -O3 -ffast-math ", | ||
optSize: " -Os -ffast-math ", | ||
compilerExe: "aarch64-none-elf-gcc", | ||
cppCompiler: "aarch64-none-elf-g++", | ||
compileTmpl: "-MMD -MP -MF $dfile -c $options $include -o $objfile $file", | ||
buildGui: " -mwindows", | ||
buildDll: " -shared", | ||
buildLib: "aarch64-none-elf-gcc-ar rcs $libfile $objfiles", | ||
linkerExe: "aarch64-none-elf-gcc", | ||
linkTmpl: "$buildgui $builddll -Wl,-Map,$mapfile -o $exefile $objfiles $options", | ||
includeCmd: " -I", | ||
linkDirCmd: " -L", | ||
linkLibCmd: " -l$1", | ||
debug: "", | ||
pic: "-fPIE", | ||
asmStmtFrmt: "asm($1);$n", | ||
structStmtFmt: "$1 $3 $2 ", # struct|union [packed] $name | ||
props: {hasSwitchRange, hasComputedGoto, hasCpp, hasGcGuard, hasGnuAsm, | ||
hasAttribute}) | ||
|
||
# LLVM Frontend for GCC/G++ | ||
compiler llvmGcc: | ||
result = gcc() # Uses settings from GCC | ||
|
@@ -316,6 +341,7 @@ compiler ucc: | |
const | ||
CC*: array[succ(low(TSystemCC))..high(TSystemCC), TInfoCC] = [ | ||
gcc(), | ||
nintendoSwitchGCC(), | ||
llvmGcc(), | ||
clang(), | ||
lcc(), | ||
|
@@ -556,14 +582,20 @@ proc getCompileCFileCmd*(conf: ConfigRef; cfile: Cfile): string = | |
else: | ||
cfile.obj | ||
|
||
# D files are required by nintendo switch libs for | ||
# compilation. They are basically a list of all includes. | ||
let dfile = objfile.replace(".o", ".d").quoteShell() | ||
|
||
objfile = quoteShell(objfile) | ||
cf = quoteShell(cf) | ||
result = quoteShell(compilePattern % [ | ||
"dfile", dfile, | ||
"file", cf, "objfile", objfile, "options", options, | ||
"include", includeCmd, "nim", getPrefixDir(conf), | ||
"nim", getPrefixDir(conf), "lib", conf.libpath]) | ||
add(result, ' ') | ||
addf(result, CC[c].compileTmpl, [ | ||
"dfile", dfile, | ||
"file", cf, "objfile", objfile, | ||
"options", options, "include", includeCmd, | ||
"nim", quoteShell(getPrefixDir(conf)), | ||
|
@@ -659,16 +691,24 @@ proc getLinkCmd(conf: ConfigRef; projectfile, objfiles: string): string = | |
if optCDebug in conf.globalOptions: | ||
writeDebugInfo(exefile.changeFileExt("ndb")) | ||
exefile = quoteShell(exefile) | ||
|
||
# Map files are required by Nintendo Switch compilation. They are a list | ||
# of all function calls in the library and where they come from. | ||
var mapfile = getNimcacheDir(conf) / splitFile(projectFile).name & ".map" | ||
mapfile = quoteShell(mapfile) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Combine these two statements. |
||
|
||
let linkOptions = getLinkOptions(conf) & " " & | ||
getConfigVar(conf, conf.cCompiler, ".options.linker") | ||
var linkTmpl = getConfigVar(conf, conf.cCompiler, ".linkTmpl") | ||
if linkTmpl.len == 0: | ||
linkTmpl = CC[conf.cCompiler].linkTmpl | ||
result = quoteShell(result % ["builddll", builddll, | ||
"mapfile", mapfile, | ||
"buildgui", buildgui, "options", linkOptions, "objfiles", objfiles, | ||
"exefile", exefile, "nim", getPrefixDir(conf), "lib", conf.libpath]) | ||
result.add ' ' | ||
addf(result, linkTmpl, ["builddll", builddll, | ||
"mapfile", mapfile, | ||
"buildgui", buildgui, "options", linkOptions, | ||
"objfiles", objfiles, "exefile", exefile, | ||
"nim", quoteShell(getPrefixDir(conf)), | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -228,6 +228,29 @@ configuration file should contain something like:: | |
arm.linux.gcc.exe = "arm-linux-gcc" | ||
arm.linux.gcc.linkerexe = "arm-linux-gcc" | ||
|
||
Cross compilation for Nintendo Switch | ||
===================================== | ||
|
||
Simply add --os:nintendoswitch | ||
to your usual ``nim c`` or ``nim cpp`` command. DevkitPro setup must be the same as | ||
what is the default with their new installer | ||
[here for Mac/Linux](https://github.com/devkitPro/pacman/releases) or | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's not how RST links look like. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, copy pasted from MD file. I'll update to be in line with rst. |
||
[here for Windows](https://github.com/devkitPro/installer/releases). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same. |
||
|
||
For example:: | ||
|
||
nim c --os:nintendoswitch switchhomebrew.nim | ||
|
||
This will generate a file called ``switchhomebrew.elf`` which can then be turned into | ||
an nro file with the ``elf2nro`` tool in the DevkitPro release. Examples can be found at | ||
[the nim-libnx github repo](https://github.com/jyapayne/nim-libnx.git). | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same. |
||
|
||
Environment variables are ``DEVKITPRO`` for the devkitpro path, ``SWITCH_LIBS`` for any extra | ||
libraries required by your application (``-lLIBNAME`` or ``-LLIBPATH``), and | ||
``SWITCH_INCLUDES`` for any extra include files (``-IINCLUDE_PATH``). | ||
|
||
There are some directories expected to exist in a specific structure for now until I figure out a better way to specify them. They are: ``DEVKITPRO/portlibs/switch/lib``, ``DEVKITPRO/libnx/lib``, | ||
``DEVKITPRO/portlibs/switch/include``, and ``DEVKITPRO/libnx/include``. | ||
|
||
DLL generation | ||
============== | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const virtMemHeader = "<switch/kernel/virtmem.h>" | ||
const svcHeader = "<switch/kernel/virtmem.h>" | ||
const mallocHeader = "<malloc.h>" | ||
|
||
type | ||
Result* = uint32 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer no type alias here. |
||
|
||
proc memalign*(bytes: csize, size: csize): pointer {.importc: "memalign", | ||
header: mallocHeader.} | ||
|
||
proc free*(address: pointer) {.importc: "free", | ||
header: mallocHeader.} | ||
|
||
proc svcMapMemory*(dst_addr: pointer; src_addr: pointer; size: uint64): Result {. | ||
importc: "svcMapMemory", header: svcHeader.} | ||
|
||
proc svcUnmapMemory*(dst_addr: pointer; src_addr: pointer; size: uint64): Result {. | ||
importc: "svcUnmapMemory", header: svcHeader.} | ||
|
||
proc virtmemReserveMap*(size: csize): pointer {.importc: "virtmemReserveMap", | ||
header: virtMemHeader.} | ||
|
||
proc virtmemFreeMap*(address: pointer; size: csize) {.importc: "virtmemFreeMap", | ||
header: virtMemHeader.} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
use
changeFileExt
instead.