-
Notifications
You must be signed in to change notification settings - Fork 0
/
nakefile.nim
executable file
·183 lines (150 loc) · 7.68 KB
/
nakefile.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import nake
import tables
let appName = "MyGame"
let bundleId = "com.mycompany.MyGame"
let javaPackageId = "com.mycompany.MyGame"
let androidSdk = "~/HDD_storage/AndroidSDK/android-sdk-linux"
let androidNdk = "~/HDD_storage/AndroidSDK/android-ndk-r12"
let sdlRoot = "~/Programming/SDL2-2.0.4"
let sdlTtfRoot = "~/Programming/SDL2_ttf-2.0.14"
# This should point to the Nim include dir, where nimbase.h resides.
# Needed for android only
let nimIncludeDir = "/home/peter/Projects/Nim/lib/" # "/usr/lib/nim"
let macOSSDKVersion = "10.11"
let macOSMinVersion = "10.6"
let iOSSDKVersion = "9.2"
let iOSMinVersion = iOSSDKVersion
# Simulator device identifier should be set to run the simulator.
# Available simulators can be listed with the command:
# $ xcrun simctl list
let iOSSimulatorDeviceId = "581E5F3F-814A-484F-B015-33B95851B947"
let bundleName = appName & ".app"
let parallelBuild = "--parallelBuild:1"
let nimVerbose = "--verbosity:0"
let xCodeApp = "/Applications/Xcode.app"
let macOSSDK = xCodeApp/"Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX" & macOSSDKVersion & ".sdk"
let iOSSDK = xCodeApp/"Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS" & iOSSDKVersion & ".sdk"
let iOSSimulatorSDK = xCodeApp/"Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator" & iOSSDKVersion & ".sdk"
proc infoPlistSetValueForKey(path, value, key: string) =
direShell "defaults", "write", path, key, value
proc absPath(path: string): string =
if path.isAbsolute(): path else: getCurrentDir() / path
proc makeBundle() =
removeDir bundleName
createDir bundleName
for t, p in walkDir("res"):
if t == pcDir:
copyDir(p, bundleName / extractFilename(p))
else:
copyFile(p, bundleName / extractFilename(p))
moveFile "main", bundleName / "main"
let infoPlistPath = absPath(bundleName / "Info")
infoPlistSetValueForKey(infoPlistPath, appName, "CFBundleName")
infoPlistSetValueForKey(infoPlistPath, bundleId, "CFBundleIdentifier")
proc symLink(source, destination: string) =
direShell "ln", "-sf", source, destination
proc createSDLIncludeLink(dir: string) =
createDir dir
symLink(sdlRoot/"include", dir/"SDL2")
symLink(sdlTtfRoot/"/external/freetype-2.4.12/include", dir/"SDL2_ttf")
proc runAppInSimulator() =
var waitForDebugger = "--wait-for-debugger"
waitForDebugger = ""
direShell "open", "-a", "Simulator"
shell "xcrun", "simctl", "uninstall", iOSSimulatorDeviceId, bundleId
direShell "xcrun", "simctl", "install", iOSSimulatorDeviceId, bundleName
direShell "xcrun", "simctl", "launch", waitForDebugger, iOSSimulatorDeviceId, bundleId
proc replaceVarsInFile(file: string, vars: Table[string, string]) =
var content = readFile(file)
for k, v in vars:
content = content.replace("$(" & k & ")", v)
writeFile(file, content)
proc buildSDLForMacOS(): string =
let xcodeProjDir = expandTilde(sdlRoot)/"Xcode/SDL"
let libDir = xcodeProjDir/"build/Release"
if not fileExists libDir/"libSDL2.a":
direShell "xcodebuild", "-project", xcodeProjDir/"SDL.xcodeproj", "-target", "Static\\ Library", "-configuration", "Release", "-sdk", "macosx"&macOSSDKVersion, "SYMROOT=build"
libDir
proc buildSDLForIOS(forSimulator: bool = false): string =
let entity = if forSimulator: "iphonesimulator" else: "iphoneos"
let xcodeProjDir = expandTilde(sdlRoot)/"Xcode-iOS/SDL"
let libDir = xcodeProjDir/"build/Release-" & entity
if not fileExists libDir/"libSDL2.a":
direShell "xcodebuild", "-project", xcodeProjDir/"SDL.xcodeproj", "-configuration", "Release", "-sdk", entity&iOSSDKVersion, "SYMROOT=build"
libDir
proc makeAndroidBuildDir(): string =
let buildDir = "android"/javaPackageId
if not dirExists buildDir:
copyDir "android/template", buildDir
symLink(sdlRoot/"src", buildDir/"jni/SDL/src")
symLink(sdlRoot/"include", buildDir/"jni/SDL/include")
echo sdlTtfRoot/"external/freetype-2.4.12/src"
symLink(sdlTtfRoot/"external/freetype-2.4.12/src", buildDir/"jni/SDL_ttf/external/freetype-2.4.12/src")
symLink(sdlTtfRoot/"external/freetype-2.4.12/include", buildDir/"jni/SDL_ttf/external/freetype-2.4.12/include")
createSDLIncludeLink(buildDir/"jni/src")
let mainActivityPath = javaPackageId.replace(".", "/")
createDir(buildDir/"src"/mainActivityPath)
let mainActivityJava = """
package """ & javaPackageId & """;
import org.libsdl.app.SDLActivity;
public class MainActivity extends SDLActivity {}
"""
writeFile(buildDir/"src"/mainActivityPath/"MainActivity.java", mainActivityJava)
let vars = {
"PACKAGE_ID" : javaPackageId,
"APP_NAME" : appName
}.toTable()
replaceVarsInFile buildDir/"AndroidManifest.xml", vars
replaceVarsInFile buildDir/"res/values/strings.xml", vars
buildDir
proc runNim(arguments: varargs[string]) =
var args = @[nimExe, "c", "--noMain", parallelBuild, "--stackTrace:off", "--lineTrace:off",
nimVerbose, "-d:noAutoGLerrorCheck"]
args.add arguments
args.add "main"
direShell args
task defaultTask, "Build and run":
createSDLIncludeLink "nimcache"
runNim "--passC:-Inimcache", "--passC:-isysroot", "--passC:" & macOSSDK, "--passL:-isysroot", "--passL:" & macOSSDK,
"--passC:-mmacosx-version-min=" & macOSMinVersion, "--passL:-mmacosx-version-min=" & macOSMinVersion,
"--passL:-fobjc-link-runtime", "-d:SDL_Static", "--passL:-L"&buildSDLForMacOS(), "--passL:-lSDL2",
"--run"
task "ios-sim", "Build and run in iOS simulator":
createSDLIncludeLink "nimcache"
runNim "--passC:-Inimcache", "--cpu:amd64", "--os:macosx", "-d:ios", "-d:iPhone", "-d:simulator", "-d:SDL_Static",
"--passC:-isysroot", "--passC:" & iOSSimulatorSDK, "--passL:-isysroot", "--passL:" & iOSSimulatorSDK,
"--passL:-L" & buildSDLForIOS(true), "--passL:-lSDL2",
"--passC:-mios-simulator-version-min=" & iOSMinVersion, "--passL:-mios-simulator-version-min=" & iOSMinVersion,
"--passL:-fobjc-link-runtime"
makeBundle()
runAppInSimulator()
task "ios", "Build for iOS":
createSDLIncludeLink "nimcache"
runNim "--passC:-Inimcache", "--cpu:arm", "--os:macosx", "-d:ios", "-d:iPhone", "-d:SDL_Static",
"--passC:-isysroot", "--passC:" & iOSSDK, "--passL:-isysroot", "--passL:" & iOSSDK,
"--passL:-L" & buildSDLForIOS(false), "--passL:-lSDL2",
"--passC:-mios-version-min=" & iOSMinVersion, "--passL:-mios-version-min=" & iOSMinVersion,
"--passL:-fobjc-link-runtime"
echo "TODO: Codesign!"
makeBundle()
task "droid", "Build for android":
let buildDir = makeAndroidBuildDir()
let droidSrcDir = buildDir / "jni/src"
runNim "--compileOnly", "--cpu:arm", "--os:linux", "-d:android", "-d:SDL_Static", "--nimcache:" & droidSrcDir
cd buildDir
putEnv "NIM_INCLUDE_DIR", expandTilde(nimIncludeDir)
direShell androidSdk/"tools/android", "update", "project", "-p", ".", "-t", "android-24"
direShell androidNdk/"ndk-build"
direShell "ant", "debug"
task "droid-debug", "Build for android":
let buildDir = makeAndroidBuildDir()
let droidSrcDir = buildDir / "jni/src"
runNim "--compileOnly", "--cpu:arm", "--os:linux", "-d:android", "-d:SDL_Static", "--nimcache:" & droidSrcDir
cd buildDir
putEnv "NIM_INCLUDE_DIR", expandTilde(nimIncludeDir)
direShell androidSdk/"tools/android", "update", "project", "-p", ".", "-t", "android-24"
direShell androidNdk/"ndk-build", "NDK_DEBUG=1"
direShell "ant", "debug"
task "droid-install", "Install to android device.":
cd makeAndroidBuildDir()
direShell "ant", "debug", "install"