Skip to content

Commit 827200f

Browse files
authored
Merge pull request #4 from leetal/new_version
Better compatibility. Possibility to expose symbols (especially useful in shared libraries)
2 parents 096778e + c5a43fc commit 827200f

File tree

8 files changed

+90
-15
lines changed

8 files changed

+90
-15
lines changed

.travis.yml

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,24 @@ matrix:
55
include:
66
- os: osx
77
compiler: clang
8-
env: IOS_PLATFORM=SIMULATOR
8+
env:
9+
- IOS_PLATFORM=SIMULATOR
10+
- BUILD_SHARED=0
911
- os: osx
1012
compiler: clang
11-
env: IOS_PLATFORM=SIMULATOR64
13+
env:
14+
- IOS_PLATFORM=SIMULATOR64
15+
- BUILD_SHARED=0
1216
- os: osx
1317
compiler: clang
14-
env: IOS_PLATFORM=OS
18+
env:
19+
- IOS_PLATFORM=OS
20+
- BUILD_SHARED=0
21+
- os: osx
22+
compiler: clang
23+
env:
24+
- IOS_PLATFORM=OS
25+
- BUILD_SHARED=1
1526

1627
install: true
1728

.travis/build.sh

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,15 @@
22

33
mkdir example/example-lib/build
44
cd example/example-lib/build
5+
6+
SHARED_EXT=""
7+
if [[ $BUILD_SHARED -eq 1 ]]; then
8+
SHARED_EXT="-DBUILD_SHARED=1 -DENABLE_VISIBILITY=1"
9+
fi
10+
511
cmake .. \
612
-DCMAKE_TOOLCHAIN_FILE=../../ios.toolchain.cmake \
7-
-DIOS_PLATFORM=$IOS_PLATFORM \
13+
-DIOS_PLATFORM=$IOS_PLATFORM $SHARED_EXT\
814
|| exit 1
9-
make || exit 1
15+
make -j2 || exit 1
1016
make install || exit 1
48.8 KB
Binary file not shown.
-10.8 KB
Binary file not shown.
47.4 KB
Binary file not shown.

example/example-lib/CMakeLists.txt

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,24 @@ set (HEADERS
1717
)
1818

1919
# Library
20-
add_library (example STATIC ${SOURCES} ${HEADERS})
20+
if (BUILD_SHARED)
21+
add_library (example SHARED ${SOURCES} ${HEADERS})
22+
message(STATUS "Building shared version...")
23+
else()
24+
add_library (example STATIC ${SOURCES} ${HEADERS})
25+
message(STATUS "Building static version...")
26+
endif()
27+
28+
# Executable
29+
add_executable (helloworld main.cpp)
30+
31+
# Link the library with the executable
32+
target_link_libraries(helloworld example)
2133

2234
# Debug symbols set in XCode project
2335
set_xcode_property (example GCC_GENERATE_DEBUGGING_SYMBOLS YES "All")
2436

2537
# Installation
2638
set (CMAKE_INSTALL_PREFIX "${example-ios_SOURCE_DIR}/../example-app/example-lib")
27-
install (TARGETS example DESTINATION lib)
39+
install (TARGETS example helloworld DESTINATION lib)
2840
install (FILES ${HEADERS} DESTINATION include)

example/example-lib/main.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <iostream>
2+
3+
#include "HelloWorld.hpp"
4+
5+
int main(int argc, char** argv)
6+
{
7+
HelloWorld hw;
8+
std::cout << hw.helloWorld() << std::endl;;
9+
}

ios.toolchain.cmake

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
#
4444
# *****************************************************************************
4545
# Now maintained by Alexander Widerberg (widerbergaren [at] gmail.com)
46-
# under the BSD-Clause-3 licence
46+
# under the BSD-3-Clause license
4747
# *****************************************************************************
4848
#
4949
# INFORMATION / HELP
@@ -65,6 +65,7 @@
6565
# not be required).
6666
# ENABLE_BITCODE: (1|0) Enables or disables bitcode support. Default 1 (true)
6767
# ENABLE_ARC: (1|0) Enables or disables ARC support. Default 1 (true, ARC enabled by default)
68+
# ENABLE_VISIBILITY: (1|0) Enables or disables symbol visibility support. Default 0 (false, visibility hidden by default)
6869
# IOS_ARCH: (armv7 armv7s arm64 i386 x86_64) If specified, will override the default architectures for the given IOS_PLATFORM
6970
# OS = armv7 armv7s arm64
7071
# SIMULATOR = i386
@@ -106,7 +107,7 @@ string(REGEX MATCH "Xcode [0-9\\.]+" XCODE_VERSION "${XCODE_VERSION}")
106107
string(REGEX REPLACE "Xcode ([0-9\\.]+)" "\\1" XCODE_VERSION "${XCODE_VERSION}")
107108
message(STATUS "Building with Xcode version: ${XCODE_VERSION}")
108109
# Default to building for iPhoneOS if not specified otherwise, and we cannot
109-
# determine the platform from the CMAKE_OSX_ARCHITECTURES variable. The use
110+
# determine the platform from the CMAKE_OSX_ARCHITECTURES variable. The use
110111
# of CMAKE_OSX_ARCHITECTURES is such that try_compile() projects can correctly
111112
# determine the value of IOS_PLATFORM from the root project, as
112113
# CMAKE_OSX_ARCHITECTURES is propagated to them by CMake.
@@ -178,17 +179,26 @@ if (NOT DEFINED IOS_DEPLOYMENT_TARGET)
178179
message(STATUS "Using the default min-version since IOS_DEPLOYMENT_TARGET not provided!")
179180
endif()
180181
# Use bitcode or not
181-
if (NOT DEFINED ENABLE_BITCODE)
182+
if (NOT DEFINED ENABLE_BITCODE AND NOT IOS_ARCH MATCHES "((^|, )(i386|x86_64))+")
182183
# Unless specified, enable bitcode support by default
183-
set(ENABLE_BITCODE TRUE CACHE BOOL "Wheter or not to enable bitcode")
184+
set(ENABLE_BITCODE TRUE CACHE BOOL "Whether or not to enable bitcode")
184185
message(STATUS "Enabling bitcode support by default. ENABLE_BITCODE not provided!")
185186
endif()
187+
if (NOT DEFINED ENABLE_BITCODE)
188+
message(STATUS "Disabling bitcode support by default on simulators. ENABLE_BITCODE not provided for override!")
189+
endif()
186190
# Use ARC or not
187191
if (NOT DEFINED ENABLE_ARC)
188192
# Unless specified, enable ARC support by default
189-
set(ENABLE_ARC TRUE CACHE BOOL "Wheter or not to enable ARC")
193+
set(ENABLE_ARC TRUE CACHE BOOL "Whether or not to enable ARC")
190194
message(STATUS "Enabling ARC support by default. ENABLE_ARC not provided!")
191195
endif()
196+
# Use hidden visibility or not
197+
if (NOT DEFINED ENABLE_VISIBILITY)
198+
# Unless specified, disable symbols visibility by default
199+
set(ENABLE_VISIBILITY FALSE CACHE BOOL "Whether or not to hide symbols (-fvisibility=hidden)")
200+
message(STATUS "Hiding symbols visibility by default. ENABLE_VISIBILITY not provided!")
201+
endif()
192202
# Get the SDK version information.
193203
execute_process(COMMAND xcodebuild -sdk ${CMAKE_OSX_SYSROOT} -version SDKVersion
194204
OUTPUT_VARIABLE IOS_SDK_VERSION
@@ -263,12 +273,27 @@ set(CMAKE_SHARED_LIBRARY_PREFIX "lib")
263273
set(CMAKE_SHARED_LIBRARY_SUFFIX ".dylib")
264274
set(CMAKE_SHARED_MODULE_PREFIX "lib")
265275
set(CMAKE_SHARED_MODULE_SUFFIX ".so")
276+
set(CMAKE_C_COMPILER_ABI ELF)
277+
set(CMAKE_CXX_COMPILER_ABI ELF)
278+
set(CMAKE_C_HAS_ISYSROOT 1)
279+
set(CMAKE_CXX_HAS_ISYSROOT 1)
266280
set(CMAKE_MODULE_EXISTS 1)
267281
set(CMAKE_DL_LIBS "")
268282
set(CMAKE_C_OSX_COMPATIBILITY_VERSION_FLAG "-compatibility_version ")
269283
set(CMAKE_C_OSX_CURRENT_VERSION_FLAG "-current_version ")
270284
set(CMAKE_CXX_OSX_COMPATIBILITY_VERSION_FLAG "${CMAKE_C_OSX_COMPATIBILITY_VERSION_FLAG}")
271285
set(CMAKE_CXX_OSX_CURRENT_VERSION_FLAG "${CMAKE_C_OSX_CURRENT_VERSION_FLAG}")
286+
287+
if(IOS_ARCH MATCHES "((^|, )(arm64|x86_64))+")
288+
set(CMAKE_C_SIZEOF_DATA_PTR 8)
289+
set(CMAKE_CXX_SIZEOF_DATA_PTR 8)
290+
message(STATUS "Using a data_ptr size of 8")
291+
else()
292+
set(CMAKE_C_SIZEOF_DATA_PTR 4)
293+
set(CMAKE_CXX_SIZEOF_DATA_PTR 4)
294+
message(STATUS "Using a data_ptr size of 4")
295+
endif()
296+
272297
message(STATUS "Building for minimum iOS version: ${IOS_DEPLOYMENT_TARGET}"
273298
" (SDK version: ${IOS_SDK_VERSION})")
274299
# Note that only Xcode 7+ supports the newer more specific:
@@ -298,9 +323,11 @@ message(STATUS "Version flags set to: ${XCODE_IOS_PLATFORM_VERSION_FLAGS}")
298323

299324
if (ENABLE_BITCODE)
300325
set(BITCODE "-fembed-bitcode")
326+
set(HEADER_PAD "")
301327
message(STATUS "Enabling bitcode support.")
302328
else()
303329
set(BITCODE "")
330+
set(HEADER_PAD "-headerpad_max_install_names")
304331
message(STATUS "Disabling bitcode support.")
305332
endif()
306333

@@ -312,11 +339,18 @@ else()
312339
message(STATUS "Disabling ARC support.")
313340
endif()
314341

342+
if (NOT ENABLE_VISIBILITY)
343+
set(VISIBILITY "-fvisibility=hidden")
344+
message(STATUS "Hiding symbols (-fvisibility=hidden).")
345+
else()
346+
set(VISIBILITY "")
347+
endif()
348+
315349
set(CMAKE_C_FLAGS
316350
"${XCODE_IOS_PLATFORM_VERSION_FLAGS} ${BITCODE} -fobjc-abi-version=2 ${FOBJC_ARC} ${C_FLAGS}")
317351
# Hidden visibilty is required for C++ on iOS.
318352
set(CMAKE_CXX_FLAGS
319-
"${XCODE_IOS_PLATFORM_VERSION_FLAGS} ${BITCODE} -fvisibility=hidden -fvisibility-inlines-hidden -fobjc-abi-version=2 ${FOBJC_ARC} ${CXX_FLAGS}")
353+
"${XCODE_IOS_PLATFORM_VERSION_FLAGS} ${BITCODE} ${VISIBILITY} -fvisibility-inlines-hidden -fobjc-abi-version=2 ${FOBJC_ARC} ${CXX_FLAGS}")
320354
set(CMAKE_CXX_FLAGS_MINSIZEREL "${CMAKE_CXX_FLAGS} -DNDEBUG -Os -fomit-frame-pointer -ffast-math ${BITCODE} ${CXX_FLAGS_MINSIZEREL}")
321355
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "${CMAKE_CXX_FLAGS} -DNDEBUG -O2 -g -fomit-frame-pointer -ffast-math ${BITCODE} ${CXX_FLAGS_RELWITHDEBINFO}")
322356
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -DNDEBUG -O3 -fomit-frame-pointer -ffast-math ${BITCODE} ${CXX_FLAGS_RELEASE}")
@@ -339,11 +373,13 @@ foreach(VAR_TO_FORCE ${VARS_TO_FORCE_IN_CACHE})
339373
endforeach()
340374

341375
set(CMAKE_PLATFORM_HAS_INSTALLNAME 1)
342-
set(CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS "-dynamiclib -headerpad_max_install_names")
343-
set(CMAKE_SHARED_MODULE_CREATE_C_FLAGS "-bundle -headerpad_max_install_names")
376+
set (CMAKE_SHARED_LINKER_FLAGS "-rpath @executable_path/Frameworks -rpath @loader_path/Frameworks")
377+
set(CMAKE_SHARED_LIBRARY_CREATE_C_FLAGS "-dynamiclib ${HEADER_PAD}")
378+
set(CMAKE_SHARED_MODULE_CREATE_C_FLAGS "-bundle ${HEADER_PAD}")
344379
set(CMAKE_SHARED_MODULE_LOADER_C_FLAG "-Wl,-bundle_loader,")
345380
set(CMAKE_SHARED_MODULE_LOADER_CXX_FLAG "-Wl,-bundle_loader,")
346381
set(CMAKE_FIND_LIBRARY_SUFFIXES ".dylib" ".so" ".a")
382+
347383
# Hack: if a new cmake (which uses CMAKE_INSTALL_NAME_TOOL) runs on an old
348384
# build tree (where install_name_tool was hardcoded) and where
349385
# CMAKE_INSTALL_NAME_TOOL isn't in the cache and still cmake didn't fail in
@@ -353,6 +389,7 @@ set(CMAKE_FIND_LIBRARY_SUFFIXES ".dylib" ".so" ".a")
353389
if (NOT DEFINED CMAKE_INSTALL_NAME_TOOL)
354390
find_program(CMAKE_INSTALL_NAME_TOOL install_name_tool)
355391
endif (NOT DEFINED CMAKE_INSTALL_NAME_TOOL)
392+
356393
# Set the find root to the iOS developer roots and to user defined paths.
357394
set(CMAKE_FIND_ROOT_PATH ${CMAKE_IOS_DEVELOPER_ROOT} ${CMAKE_OSX_SYSROOT}
358395
${CMAKE_PREFIX_PATH} CACHE string "iOS find search path root" FORCE)

0 commit comments

Comments
 (0)