Browse Source

Use Cmake function to determine whether to add compiler export flags.

Yao Wei Tjong 姚伟忠 11 years ago
parent
commit
18261e3d2a

+ 2 - 5
CMake/Modules/GenerateExportHeader.cmake

@@ -348,11 +348,8 @@ macro(_DO_GENERATE_EXPORT_HEADER TARGET_LIBRARY)
 
 
   set(INCLUDE_GUARD_NAME "${EXPORT_MACRO_NAME}_H")
   set(INCLUDE_GUARD_NAME "${EXPORT_MACRO_NAME}_H")
 
 
-  get_target_property(EXPORT_IMPORT_CONDITION ${TARGET_LIBRARY} DEFINE_SYMBOL)
-  
-  if(NOT EXPORT_IMPORT_CONDITION)
-    set(EXPORT_IMPORT_CONDITION ${TARGET_LIBRARY}_EXPORTS)
-  endif()
+  # Urho3D: Our revised version does not depend on the target to be added first, so always derive the variable value from target name instead
+  set(EXPORT_IMPORT_CONDITION ${TARGET_LIBRARY}_EXPORTS)
   #string(MAKE_C_IDENTIFIER ${EXPORT_IMPORT_CONDITION} EXPORT_IMPORT_CONDITION)
   #string(MAKE_C_IDENTIFIER ${EXPORT_IMPORT_CONDITION} EXPORT_IMPORT_CONDITION)
 
 
   configure_file("${_GENERATE_EXPORT_HEADER_MODULE_DIR}/exportheader.cmake.in"
   configure_file("${_GENERATE_EXPORT_HEADER_MODULE_DIR}/exportheader.cmake.in"

+ 17 - 5
CMake/Modules/Urho3D-CMake-common.cmake

@@ -492,8 +492,9 @@ macro (enable_pch)
                         string (MAKE_C_IDENTIFIER ${EXPORT_IMPORT_CONDITION} EXPORT_IMPORT_CONDITION)
                         string (MAKE_C_IDENTIFIER ${EXPORT_IMPORT_CONDITION} EXPORT_IMPORT_CONDITION)
                     endif ()
                     endif ()
                     list (APPEND COMPILE_DEFINITIONS ${EXPORT_IMPORT_CONDITION})
                     list (APPEND COMPILE_DEFINITIONS ${EXPORT_IMPORT_CONDITION})
-                    # Below is a hack as it assumes the compiler is modern enough to support these flags
-                    set (COMPILER_EXPORT_FLAGS "-fPIC -fvisibility=hidden -fvisibility-inlines-hidden")
+                    # todo: replace the usage of this deprecated function (since CMake 2.8.12) later
+                    add_compiler_export_flags (COMPILER_EXPORT_FLAGS)
+                    set (COMPILER_EXPORT_FLAGS "${COMPILER_EXPORT_FLAGS} -fPIC")
                 endif ()
                 endif ()
                 string (REPLACE ";" " -D" COMPILE_DEFINITIONS "-D${COMPILE_DEFINITIONS}")
                 string (REPLACE ";" " -D" COMPILE_DEFINITIONS "-D${COMPILE_DEFINITIONS}")
                 string (REPLACE ";" " -I" INCLUDE_DIRECTORIES "-I${INCLUDE_DIRECTORIES}")
                 string (REPLACE ";" " -I" INCLUDE_DIRECTORIES "-I${INCLUDE_DIRECTORIES}")
@@ -575,15 +576,26 @@ macro (check_source_files)
 endmacro ()
 endmacro ()
 
 
 # Macro for setting up a library target
 # Macro for setting up a library target
+#  STATIC/SHARED/MODULE/EXCLUDE_FROM_ALL - see CMake help on add_library() command
 macro (setup_library)
 macro (setup_library)
     check_source_files ()
     check_source_files ()
     add_library (${TARGET_NAME} ${ARGN} ${SOURCE_FILES})
     add_library (${TARGET_NAME} ${ARGN} ${SOURCE_FILES})
     setup_target ()
     setup_target ()
 
 
+    # Setup the compiler flags for building shared library
+    get_target_property (LIB_TYPE ${TARGET_NAME} TYPE)
+    if (LIB_TYPE MATCHES SHARED)
+        # Hide the symbols that are not explicitly marked for export
+        if (CMAKE_VERSION VERSION_LESS 2.8.12)
+            add_compiler_export_flags ()    # todo: Remove this deprecated function once CMake minimum version is 2.8.12
+        else ()
+            set_target_properties (${TARGET_NAME} PROPERTIES CXX_VISIBILITY_PRESET hidden VISIBILITY_INLINES_HIDDEN 1)
+        endif ()
+    endif ()
+
     if (CMAKE_PROJECT_NAME STREQUAL Urho3D)
     if (CMAKE_PROJECT_NAME STREQUAL Urho3D)
         if (NOT ${TARGET_NAME} STREQUAL Urho3D)
         if (NOT ${TARGET_NAME} STREQUAL Urho3D)
             # Only interested in static library type, i.e. exclude shared and module library types
             # Only interested in static library type, i.e. exclude shared and module library types
-            get_target_property (LIB_TYPE ${TARGET_NAME} TYPE)
             if (LIB_TYPE MATCHES STATIC)
             if (LIB_TYPE MATCHES STATIC)
                 set (STATIC_LIBRARY_TARGETS ${STATIC_LIBRARY_TARGETS} ${TARGET_NAME} PARENT_SCOPE)
                 set (STATIC_LIBRARY_TARGETS ${STATIC_LIBRARY_TARGETS} ${TARGET_NAME} PARENT_SCOPE)
             endif ()
             endif ()
@@ -598,7 +610,7 @@ include(CMakeParseArguments)
 
 
 # Macro for setting up an executable target
 # Macro for setting up an executable target
 #  NODEPS - setup executable target without defining Urho3D dependency libraries
 #  NODEPS - setup executable target without defining Urho3D dependency libraries
-#  WIN32/MACOSX_BUNDLE/EXCLUDE_FROM_ALL - see CMake help on add_executable command
+#  WIN32/MACOSX_BUNDLE/EXCLUDE_FROM_ALL - see CMake help on add_executable() command
 macro (setup_executable)
 macro (setup_executable)
     # Parse extra arguments
     # Parse extra arguments
     cmake_parse_arguments (ARG "NODEPS" "" "" ${ARGN})
     cmake_parse_arguments (ARG "NODEPS" "" "" ${ARGN})
@@ -635,7 +647,7 @@ endmacro ()
 # Macro for setting up an executable target with resources to copy
 # Macro for setting up an executable target with resources to copy
 #  NODEPS - setup executable target without defining Urho3D dependency libraries
 #  NODEPS - setup executable target without defining Urho3D dependency libraries
 #  NOBUNDLE - do not use MACOSX_BUNDLE even when URHO3D_MACOSX_BUNDLE build option is enabled
 #  NOBUNDLE - do not use MACOSX_BUNDLE even when URHO3D_MACOSX_BUNDLE build option is enabled
-#  WIN32/MACOSX_BUNDLE/EXCLUDE_FROM_ALL - see CMake help on add_executable command
+#  WIN32/MACOSX_BUNDLE/EXCLUDE_FROM_ALL - see CMake help on add_executable() command
 macro (setup_main_executable)
 macro (setup_main_executable)
     # Parse extra arguments
     # Parse extra arguments
     cmake_parse_arguments (ARG "NOBUNDLE;MACOSX_BUNDLE;WIN32" "" "" ${ARGN})
     cmake_parse_arguments (ARG "NOBUNDLE;MACOSX_BUNDLE;WIN32" "" "" ${ARGN})

+ 12 - 15
Source/Urho3D/CMakeLists.txt

@@ -254,6 +254,13 @@ if (CMAKE_VERSION VERSION_LESS 2.8.12)
     endif ()
     endif ()
 endif ()
 endif ()
 # end todo
 # end todo
+# Use PIC on platforms that support it (shared library type has this property set to true by default, so we only have to deal with those static ones that the shared library links against)
+if (URHO3D_LIB_TYPE STREQUAL SHARED)
+    set_target_properties (${STATIC_LIBRARY_TARGETS} PROPERTIES POSITION_INDEPENDENT_CODE true)
+    if (NOT MSVC AND NOT (CMAKE_CROSSCOMPILING AND MINGW) AND CMAKE_VERSION VERSION_LESS 2.8.9)  # todo: Remove this when CMake minimum version is 2.8.9
+        set_property (TARGET ${STATIC_LIBRARY_TARGETS} APPEND PROPERTY COMPILE_FLAGS -fPIC)
+    endif ()
+endif ()
 
 
 # Install headers for using the Urho3D library
 # Install headers for using the Urho3D library
 install_header_files (DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/ DESTINATION ${DEST_INCLUDE_DIR} FILES_MATCHING PATTERN *.h USE_FILE_SYMLINK)    # Note: the trailing slash is significant
 install_header_files (DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/ DESTINATION ${DEST_INCLUDE_DIR} FILES_MATCHING PATTERN *.h USE_FILE_SYMLINK)    # Note: the trailing slash is significant
@@ -301,27 +308,17 @@ if (URHO3D_LUA)
     # ditto for Lua/LuaJIT
     # ditto for Lua/LuaJIT
     set (ENGINE_INCLUDE_DIRS "${ENGINE_INCLUDE_DIRS} ${DASH}I\"\${includedir}/${PATH_SUFFIX}/ThirdParty/Lua${JIT}\"")
     set (ENGINE_INCLUDE_DIRS "${ENGINE_INCLUDE_DIRS} ${DASH}I\"\${includedir}/${PATH_SUFFIX}/ThirdParty/Lua${JIT}\"")
 endif ()
 endif ()
+if (CMAKE_VERSION VERSION_LESS 2.8.12)  # todo: Remove this regex replacement when CMake minimum version is 2.8.12, use @CMAKE_CXX_FLAGS@ directly in Urho3D.pc.in file
+    string (REGEX REPLACE " -fvisibility[^ ]+" "" CLEANED_CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})   # Remove visibility compiler options that are only used for building the library
+else ()
+    set (CLEANED_CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
+endif ()
 configure_file (${CMAKE_CURRENT_SOURCE_DIR}/Urho3D.pc.in ${CMAKE_CURRENT_BINARY_DIR}/Urho3D.pc @ONLY)
 configure_file (${CMAKE_CURRENT_SOURCE_DIR}/Urho3D.pc.in ${CMAKE_CURRENT_BINARY_DIR}/Urho3D.pc @ONLY)
 if (ANDROID)
 if (ANDROID)
     set (RENAME RENAME Urho3D-${ANDROID_NDK_ABI_NAME}.pc)
     set (RENAME RENAME Urho3D-${ANDROID_NDK_ABI_NAME}.pc)
 endif ()
 endif ()
 install (FILES ${CMAKE_CURRENT_BINARY_DIR}/Urho3D.pc DESTINATION ${DEST_PKGCONFIG_DIR} ${RENAME})
 install (FILES ${CMAKE_CURRENT_BINARY_DIR}/Urho3D.pc DESTINATION ${DEST_PKGCONFIG_DIR} ${RENAME})
 
 
-# Setup the compiler flags for building shared library (do this here so that it does not interfere with the pkg-config file generation above)
-if (URHO3D_LIB_TYPE STREQUAL SHARED)
-    # Hide the symbols that are not explicitly marked for export
-    if (CMAKE_VERSION VERSION_LESS 2.8.12)
-        add_compiler_export_flags ()    # todo: Remove this deprecated function once CMake minimum version is 2.8.12
-    else ()
-        set_target_properties (${TARGET_NAME} PROPERTIES CXX_VISIBILITY_PRESET hidden VISIBILITY_INLINES_HIDDEN 1)
-    endif ()
-    # Use PIC on platforms that support it (shared library type has this property set to true by default, so we only have to deal with those static ones that the shared library links against)
-    set_target_properties (${STATIC_LIBRARY_TARGETS} PROPERTIES POSITION_INDEPENDENT_CODE true)
-    if (NOT MSVC AND NOT (CMAKE_CROSSCOMPILING AND MINGW) AND CMAKE_VERSION VERSION_LESS 2.8.9)  # todo: Remove this when CMake minimum version is 2.8.9
-        set_property (TARGET ${STATIC_LIBRARY_TARGETS} APPEND PROPERTY COMPILE_FLAGS -fPIC)
-    endif ()
-endif ()
-
 # Define post build steps
 # Define post build steps
 if (ANDROID_NDK_GDB)
 if (ANDROID_NDK_GDB)
     # Copy the library while it still has debug symbols for ndk-gdb
     # Copy the library while it still has debug symbols for ndk-gdb

+ 1 - 1
Source/Urho3D/Urho3D.pc.in

@@ -35,4 +35,4 @@ Description: @URHO3D_DESCRIPTION@
 Version: @URHO3D_VERSION@
 Version: @URHO3D_VERSION@
 URL: @URHO3D_URL@
 URL: @URHO3D_URL@
 Libs: @CMAKE_EXE_LINKER_FLAGS@ @URHO3D_ABS_PATH_LIBS@ @LIB_DIR@ @URHO3D_LIBS@
 Libs: @CMAKE_EXE_LINKER_FLAGS@ @URHO3D_ABS_PATH_LIBS@ @LIB_DIR@ @URHO3D_LIBS@
-Cflags: @URHO3D_COMPILE_DEFINITIONS@ @CMAKE_CXX_FLAGS@ @GLOBAL_INCLUDE_DIRS@ @ENGINE_INCLUDE_DIRS@
+Cflags: @URHO3D_COMPILE_DEFINITIONS@ @CLEANED_CMAKE_CXX_FLAGS@ @GLOBAL_INCLUDE_DIRS@ @ENGINE_INCLUDE_DIRS@