1 # This file is part of NIT ( http://www.nitlanguage.org )
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
15 # Compile programs for the iOS platform
19 import compiler
::abstract_compiler
20 import xcode_templates
21 import app_annotations
23 redef class ToolContext
24 redef fun platform_from_name
(name
)
26 if name
== "ios" then return new IOSPlatform
31 private class IOSPlatform
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
)
39 private class IOSToolchain
40 super MakefileToolchain
42 # Root of the iOS project, usually `.nit_compile/ios/`
43 var ios_project_root
: String is noinit
45 # `app.nit` project for the current compilation target
46 var app_project
= new AppProject(compiler
.modelbuilder
, compiler
.mainmodule
) is lazy
48 redef fun default_outname
do return "{super}.app"
50 # Compile C files in `ios_project_root/app_project.name`
53 ios_project_root
= super/"ios"
54 return ios_project_root
/app_project
.short_name
57 redef fun write_files
(compile_dir
, cfiles
)
59 # Clear the project directory before writing anything
60 if ios_project_root
.file_exists
then ios_project_root
.rmdir
66 redef fun write_makefile
(compile_dir
, cfiles
)
68 var project_name
= app_project
.short_name
70 # Create an XCode project directory
71 var dir
= ios_project_root
/project_name
+".xcodeproj"
72 if not dir
.file_exists
then dir
.mkdir
74 # Create a PBX project file
75 var pbx
= new PbxprojectTemplate(project_name
)
77 ## Register all source files
78 for file
in cfiles
do pbx
.add_file
new PbxFile(file
)
79 for file
in compiler
.extern_bodies
do
80 pbx
.add_file
new PbxFile(file
.filename
.basename
(""))
83 ## TODO Register asset files
85 pbx
.write_to_file dir
/"project.pbxproj"
87 # Create the plist in the same directory as the generated C code
88 if not compile_dir
.file_exists
then compile_dir
.mkdir
89 var plist
= new PlistTemplate(app_project
.name
, app_project
.namespace
,
90 app_project
.version
, app_project
.version_code
.to_s
)
91 plist
.write_to_file compile_dir
/"Info.plist"
94 redef fun compile_c_code
(compile_dir
)
96 var project_name
= app_project
.short_name
97 var release
= toolcontext
.opt_release
.value
98 var outfile
= outfile
(compiler
.mainmodule
)
100 # Compile with `xcodebuild`
102 # TODO support more than the iPhone and the simulator.
103 var args
= ["sh", "-c", "cd {ios_project_root}; " +
104 "xcodebuild -target '{project_name}' " +
105 "-destination 'platform=iOS Simulator,name=iPhone' " +
106 "-configuration {if release then "Release" else "Debug"} " +
107 "-sdk iphonesimulator build"]
108 toolcontext
.exec_and_check
(args
, "iOS project error")
110 # Move compiled app to destination
111 if outfile
.file_exists
then outfile
.rmdir
112 args
= ["mv", "{ios_project_root}/build/Debug-iphonesimulator/{project_name}.app", outfile
]
113 toolcontext
.exec_and_check
(args
, "iOS project error")