Skip to content

Commit fc549e0

Browse files
committed
Adding support for iOS.
1 parent 6287a16 commit fc549e0

8 files changed

Lines changed: 141 additions & 13 deletions

File tree

.travis.yml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ matrix:
2828
# OS X xcode8
2929
- os: osx
3030
osx_image: xcode8.3
31-
env: COMPILER=xcode
31+
env: COMPILER=osx
3232

3333
# OS X xcode9
3434
- os: osx
3535
osx_image: xcode9.2
36-
env: COMPILER=xcode
36+
env: COMPILER=osx
3737

3838
before_install:
3939
- if [ "$COMPILER" = "clang4" ]; then curl -sSL "http://apt.llvm.org/llvm-snapshot.gpg.key" | sudo -E apt-key add - ; fi
@@ -50,3 +50,5 @@ script:
5050
- python3 make.py -clean -build -unit_test -${COMPILER} -Release -x86
5151
- python3 make.py -clean -build -unit_test -${COMPILER} -Debug -x64
5252
- python3 make.py -clean -build -unit_test -${COMPILER} -Release -x64
53+
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then python3 make.py -clean -build -ios -Debug ; fi
54+
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then python3 make.py -clean -build -ios -Release; fi

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ Unicode formats other than UTF-8 aren't supported.
3838
* Linux (gcc5, gcc6, gcc7, clang4, clang5) x86 and x64
3939
* OS X (Xcode 8.3, Xcode 9.2) x86 and x64
4040
* Android (NVIDIA CodeWorks) ARMv7-A
41+
* iOS (Xcode 8.3, Xcode 9.2) ARM64
42+
43+
The above supported platform list is only what is tested every release but if it compiles, it should run just fine.
4144

4245
## External dependencies
4346

@@ -59,6 +62,10 @@ See [here](./external) for details on the ones we do include.
5962

6063
For Android, the steps are identical to Windows, Linux, and OS X but you also need to install NVIDIA CodeWorks 1R5 (or higher).
6164

65+
### iOS
66+
67+
For iOS, the steps are identical to the other platforms but due to code signing, you will need to perform the builds from Xcode manually. Note that this is only an issue if you attempt to use the tools or run the unit tests locally.
68+
6269
## Authors
6370

6471
* [Nicholas Frechette](https://github.com/nfrechette)

cmake/CMakePlatforms.cmake

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ if( ${CMAKE_SYSTEM_NAME} STREQUAL "Windows" )
55
set(PLATFORM_WINDOWS 1)
66
set(PLATFORM_NAME "Windows")
77
elseif( ${CMAKE_SYSTEM_NAME} STREQUAL "Darwin" )
8-
set(PLATFORM_OSX 1)
9-
set(PLATFORM_NAME "OS X")
8+
if(PLATFORM_IOS)
9+
set(PLATFORM_NAME "iOS")
10+
else()
11+
set(PLATFORM_OSX 1)
12+
set(PLATFORM_NAME "OS X")
13+
endif()
1014
elseif( ${CMAKE_SYSTEM_NAME} STREQUAL "Linux" )
1115
set(PLATFORM_LINUX 1)
1216
set(PLATFORM_NAME "Linux")

cmake/Toolchain-iOS.cmake

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
cmake_minimum_required (VERSION 3.2)
2+
3+
set(CMAKE_SYSTEM_NAME Darwin)
4+
5+
# Set here instead of CMakePlatforms.cmake since we can't distinguis otherwise
6+
set(PLATFORM_IOS 1)
7+
8+
# Find and set the C/C++ compiler paths, cmake doesn't seem to do this properly on its own
9+
execute_process(COMMAND xcrun --sdk iphoneos --find clang OUTPUT_VARIABLE CMAKE_C_COMPILER OUTPUT_STRIP_TRAILING_WHITESPACE)
10+
execute_process(COMMAND xcrun --sdk iphoneos --find clang++ OUTPUT_VARIABLE CMAKE_CXX_COMPILER OUTPUT_STRIP_TRAILING_WHITESPACE)
11+
12+
set(CMAKE_MACOSX_BUNDLE YES)
13+
set(CMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_REQUIRED NO)
14+
set(CMAKE_OSX_SYSROOT iphoneos CACHE STRING "")
15+
set(CMAKE_OSX_ARCHITECTURES arm64 CACHE STRING "")
16+
set(CMAKE_XCODE_ATTRIBUTE_IPHONEOS_DEPLOYMENT_TARGET 11.0)

make.py

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,11 @@ def parse_argv():
6060
if value == '-gcc7':
6161
options['compiler'] = 'gcc7'
6262

63-
if value == '-xcode':
64-
options['compiler'] = 'xcode'
63+
if value == '-osx':
64+
options['compiler'] = 'osx'
65+
66+
if value == '-ios':
67+
options['compiler'] = 'ios'
6568

6669
# TODO: Refactor to use the form: -config=Release
6770
if value_upper == '-DEBUG':
@@ -77,6 +80,21 @@ def parse_argv():
7780
if value == '-x64':
7881
options['cpu'] = 'x64'
7982

83+
# Sanitize and validate our options
84+
if options['compiler'] == 'android':
85+
options['cpu'] = 'armv7-a'
86+
87+
if not platform.system() == 'Windows':
88+
print('Android is only supported on Windows')
89+
sys.exit(1)
90+
91+
if options['compiler'] == 'ios':
92+
options['cpu'] = 'arm64'
93+
94+
if not platform.system() == 'Darwin':
95+
print('iOS is only supported on OS X')
96+
sys.exit(1)
97+
8098
return options
8199

82100
def get_cmake_exes():
@@ -103,14 +121,23 @@ def get_generator(compiler, cpu):
103121
elif compiler == 'android':
104122
return 'Visual Studio 14'
105123
elif platform.system() == 'Darwin':
106-
if compiler == 'xcode':
124+
if compiler == 'osx' or compiler == 'ios':
107125
return 'Xcode'
108126
else:
109127
return 'Unix Makefiles'
110128

111129
print('Unknown compiler: {}'.format(compiler))
112130
sys.exit(1)
113131

132+
def get_toolchain(compiler):
133+
if platform.system() == 'Windows' and compiler == 'android':
134+
return 'Toolchain-Android.cmake'
135+
elif platform.system() == 'Darwin' and compiler == 'ios':
136+
return 'Toolchain-iOS.cmake'
137+
138+
# No toolchain
139+
return None
140+
114141
def set_compiler_env(compiler, options):
115142
if platform.system() == 'Linux':
116143
os.environ['MAKEFLAGS'] = '-j{}'.format(options['num_threads'])
@@ -141,15 +168,16 @@ def do_generate_solution(cmake_exe, build_dir, cmake_script_dir, options):
141168
if not compiler == None:
142169
set_compiler_env(compiler, options)
143170

144-
extra_switches = []
171+
extra_switches = ['--no-warn-unused-cli']
145172
if not platform.system() == 'Windows':
146173
extra_switches.append('-DCPU_INSTRUCTION_SET:STRING={}'.format(cpu))
147174

148175
if not platform.system() == 'Windows' and not platform.system() == 'Darwin':
149176
extra_switches.append('-DCMAKE_BUILD_TYPE={}'.format(config.upper()))
150177

151-
if platform.system() == 'Windows' and compiler == 'android':
152-
extra_switches.append('-DCMAKE_TOOLCHAIN_FILE={} --no-warn-unused-cli'.format(os.path.join(cmake_script_dir, 'Toolchain-Android.cmake')))
178+
toolchain = get_toolchain(compiler)
179+
if not toolchain == None:
180+
extra_switches.append('-DCMAKE_TOOLCHAIN_FILE={}'.format(os.path.join(cmake_script_dir, toolchain)))
153181

154182
# Generate IDE solution
155183
print('Generating build files ...')
@@ -176,7 +204,10 @@ def do_build(cmake_exe, options):
176204
else:
177205
cmake_cmd += ' --config {} --target INSTALL'.format(config)
178206
elif platform.system() == 'Darwin':
179-
cmake_cmd += ' --config {} --target install'.format(config)
207+
if options['compiler'] == 'ios':
208+
cmake_cmd += ' --config {}'.format(config)
209+
else:
210+
cmake_cmd += ' --config {} --target install'.format(config)
180211
else:
181212
cmake_cmd += ' --target install'
182213

@@ -227,8 +258,7 @@ def do_tests(ctest_exe, options):
227258
if not compiler == None:
228259
print('Using compiler: {}'.format(compiler))
229260

230-
if options['build'] or not options['unit_test']:
231-
do_generate_solution(cmake_exe, build_dir, cmake_script_dir, options)
261+
do_generate_solution(cmake_exe, build_dir, cmake_script_dir, options)
232262

233263
if options['build']:
234264
do_build(cmake_exe, options)

tests/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ project(sjson-cpp_unit_tests_root)
33

44
if(PLATFORM_ANDROID)
55
add_subdirectory("${PROJECT_SOURCE_DIR}/main_android")
6+
elseif(PLATFORM_IOS)
7+
add_subdirectory("${PROJECT_SOURCE_DIR}/main_ios")
68
else()
79
add_subdirectory("${PROJECT_SOURCE_DIR}/main_generic")
810
endif()

tests/main_ios/CMakeLists.txt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
cmake_minimum_required (VERSION 3.2)
2+
project(sjson-cpp_unit_tests)
3+
4+
# iOS cmake toolchain does not support CMAKE_CXX_STANDARD
5+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
6+
7+
# Force enable debug symbols
8+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
9+
10+
# Enable optimizations in Release
11+
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")
12+
13+
set(MACOSX_BUNDLE_EXECUTABLE_NAME ${PROJECT_NAME})
14+
set(MACOSX_BUNDLE_INFO_STRING "com.sjson.cpp.unit-tests")
15+
set(MACOSX_BUNDLE_GUI_IDENTIFIER "com.sjson.cpp.unit-tests")
16+
set(MACOSX_BUNDLE_BUNDLE_NAME "sjson-cpp-unit-tests")
17+
18+
include_directories("${PROJECT_SOURCE_DIR}/../../includes")
19+
include_directories("${PROJECT_SOURCE_DIR}/../../external/catch-1.9.6")
20+
21+
# Grab all of our test source files
22+
file(GLOB_RECURSE ALL_TEST_SOURCE_FILES LIST_DIRECTORIES false
23+
${PROJECT_SOURCE_DIR}/../sources/*.h
24+
${PROJECT_SOURCE_DIR}/../sources/*.cpp)
25+
26+
create_source_groups("${ALL_TEST_SOURCE_FILES}" ${PROJECT_SOURCE_DIR}/..)
27+
28+
# Grab all of our main source files
29+
file(GLOB_RECURSE ALL_MAIN_SOURCE_FILES LIST_DIRECTORIES false
30+
${PROJECT_SOURCE_DIR}/*.cpp)
31+
32+
create_source_groups("${ALL_MAIN_SOURCE_FILES}" ${PROJECT_SOURCE_DIR})
33+
34+
add_executable(${PROJECT_NAME} MACOSX_BUNDLE ${ALL_TEST_SOURCE_FILES} ${ALL_MAIN_SOURCE_FILES})

tests/main_ios/main.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
////////////////////////////////////////////////////////////////////////////////
2+
// The MIT License (MIT)
3+
//
4+
// Copyright (c) 2018 Nicholas Frechette, Cody Jones, and sjson-cpp contributors
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files (the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be included in all
14+
// copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
// SOFTWARE.
23+
////////////////////////////////////////////////////////////////////////////////
24+
25+
#define CATCH_CONFIG_RUNNER
26+
#include <catch.hpp>
27+
28+
int main(int argc, char* argv[])
29+
{
30+
int result = Catch::Session().run(argc, argv);
31+
32+
return (result < 0xff ? result : 0xff);
33+
}

0 commit comments

Comments
 (0)