nitc: support compiling for the iOS platform
[nit.git] / src / platform / ios.nit
1 # This file is part of NIT ( http://www.nitlanguage.org )
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Compile programs for the iOS platform
16 module ios
17
18 import platform
19 import compiler::abstract_compiler
20 import xcode_templates
21 private import annotation
22
23 redef class ToolContext
24 redef fun platform_from_name(name)
25 do
26 if name == "ios" then return new IOSPlatform
27 return super
28 end
29 end
30
31 private class IOSPlatform
32 super Platform
33
34 redef fun supports_libunwind do return false
35 redef fun supports_libgc do return false
36 redef fun toolchain(toolcontext, compiler) do return new IOSToolchain(toolcontext, compiler)
37 end
38
39 private class IOSToolchain
40 super MakefileToolchain
41
42 # Root of the iOS project, usually `.nit_compile/ios/`
43 var ios_project_root: String is noinit
44
45 redef fun default_outname do return "{super}.app"
46
47 # Name of the current project of `compiler`
48 fun project_name: String
49 do
50 var project_name = null
51 # TODO unite the app_name annotation from Android with iOS
52 var annot = compiler.modelbuilder.lookup_annotation_on_modules("app_name", compiler.mainmodule)
53 if annot != null then project_name = annot.arg_as_string(compiler.modelbuilder)
54 if project_name == null then project_name = compiler.mainmodule.name
55 return project_name
56 end
57
58 # Compile C files in `ios_project_root/project_name`
59 redef fun compile_dir
60 do
61 ios_project_root = super/"ios"
62 return ios_project_root/project_name
63 end
64
65 redef fun write_files(compile_dir, cfiles)
66 do
67 # Clear the project directory before writing anything
68 if ios_project_root.file_exists then ios_project_root.rmdir
69 compile_dir.mkdir
70
71 super
72 end
73
74 redef fun write_makefile(compile_dir, cfiles)
75 do
76 var project_name = project_name
77
78 # Create an XCode project directory
79 var dir = ios_project_root/project_name+".xcodeproj"
80 if not dir.file_exists then dir.mkdir
81
82 # Create a PBX project file
83 var pbx = new PbxprojectTemplate(project_name)
84
85 ## Register all source files
86 for file in cfiles do pbx.add_file new PbxFile(file)
87 for file in compiler.extern_bodies do
88 pbx.add_file new PbxFile(file.filename.basename(""))
89 end
90
91 ## TODO Register asset files
92
93 pbx.write_to_file dir/"project.pbxproj"
94
95 # Create the plist in the same directory as the generated C code
96 if not compile_dir.file_exists then compile_dir.mkdir
97 var plist = new PlistTemplate("org.nitlanguage") # TODO customize using an annotation
98 plist.write_to_file compile_dir/"Info.plist"
99 end
100
101 redef fun compile_c_code(compile_dir)
102 do
103 var project_name = project_name
104 var release = toolcontext.opt_release.value
105 var outfile = outfile(compiler.mainmodule)
106
107 # Compile with `xcodebuild`
108 #
109 # TODO support more than the iPhone and the simulator.
110 var args = ["sh", "-c", "cd {ios_project_root}; " +
111 "xcodebuild -target '{project_name}' " +
112 "-destination 'platform=iOS Simulator,name=iPhone' " +
113 "-configuration {if release then "Release" else "Debug"} " +
114 "-sdk iphonesimulator build"]
115 toolcontext.exec_and_check(args, "iOS project error")
116
117 # Move compiled app to destination
118 if outfile.file_exists then outfile.rmdir
119 args = ["mv", "{ios_project_root}/build/Debug-iphonesimulator/{project_name}.app", outfile]
120 toolcontext.exec_and_check(args, "iOS project error")
121 end
122 end