Browse Source

Attempt to use try_run logic to auto-discover build options.
For AppVeyor - let URHO3D_D3D11 be auto-discovered when scaffolding.
Close #1066.

Yao Wei Tjong 姚伟忠 10 years ago
parent
commit
68e2813298

+ 1 - 3
.appveyor.yml

@@ -35,8 +35,6 @@ environment:
   matrix:
     - URHO3D_LIB_TYPE: STATIC
     - URHO3D_LIB_TYPE: SHARED
-matrix:
-  fast_finish: true
 # Uncomment the init script below in order to get the RDP connection details to login into the worker box remotely while it is running
 #init:
 # - ps: iex ((new-object net.webclient).DownloadString('https://raw.githubusercontent.com/appveyor/ci/master/scripts/enable-rdp.ps1'))
@@ -55,7 +53,7 @@ build_script:
   - rake cmake vs2015 %OPTS% URHO3D_LIB_TYPE=%URHO3D_LIB_TYPE% URHO3D_D3D11=1 URHO3D_LUAJIT=1 URHO3D_LUAJIT_AMALG=1 URHO3D_EXTRAS=1 URHO3D_TESTING=1 && rake make
 # Could not run test target yet as AppVeyor does not currently allow its service to interact with desktop
 # - rake make target=RUN_TESTS
-  - if "%PACKAGE_UPLOAD%" == "1" (echo "Packaging the build artifact..." && rake ci_appveyor_package_upload) else (echo Configuring downstream project using Urho3D library in its build tree... && rake scaffolding dir=UsingBuildTree >nul && cd UsingBuildTree && rake cmake vs2015 %OPTS% URHO3D_HOME=..\..\native-Build URHO3D_D3D11=1 && rake make && echo Installing Urho3D SDK to default system-wide location... && cd .. && rake make target=install >nul && echo Configuring downstream project using Urho3D SDK... && rake scaffolding dir=UsingSDK >null && cd UsingSDK && rake cmake vs2015 %OPTS% URHO3D_D3D11=1 && rake make)
+  - if "%PACKAGE_UPLOAD%" == "1" (echo "Packaging the build artifact..." && rake ci_appveyor_package_upload) else (echo Configuring downstream project using Urho3D library in its build tree... && rake scaffolding dir=UsingBuildTree >nul && cd UsingBuildTree && rake cmake vs2015 %OPTS% URHO3D_HOME=..\..\native-Build && rake make && echo Installing Urho3D SDK to default system-wide location... && cd .. && rake make target=install >nul && echo Configuring downstream project using Urho3D SDK... && rake scaffolding dir=UsingSDK >null && cd UsingSDK && rake cmake vs2015 %OPTS% && rake make)
 test: off
 artifacts:
   - path: native-Build\*.zip

+ 10 - 1
CMake/Modules/CheckUrho3DLibrary.cpp

@@ -27,5 +27,14 @@ using namespace Urho3D;
 
 int main(int argc, char* argv[])
 {
-    std::cout << GetRevision();
+    std::cout << GetRevision() << "\n";
+
+#ifdef URHO3D_OPENGL
+    std::cout << "#define URHO3D_OPENGL\n";
+#elif defined(URHO3D_D3D11)
+    std::cout << "#define URHO3D_D3D11\n";
+#endif
+#ifdef URHO3D_SSE
+    std::cout << "#define URHO3D_SSE\n";
+#endif
 }

+ 138 - 74
CMake/Modules/FindUrho3D.cmake

@@ -32,7 +32,8 @@
 #  URHO3D_INCLUDE_DIRS
 #  URHO3D_LIBRARIES
 #  URHO3D_VERSION
-#  URHO3D_LIB_TYPE (use as input variable if specified; or as output variable if it is set based on module's search result)
+#  URHO3D_64BIT (may be used as input variable for multilib-capable compilers; must be anyway specified for MSVC due to CMake/VS generator limitation)
+#  URHO3D_LIB_TYPE (may be used as input variable as well to limit the search of library type)
 #
 # WIN32 only:
 #  URHO3D_LIBRARIES_REL
@@ -43,7 +44,6 @@
 #
 
 set (PATH_SUFFIX Urho3D)
-set (COMPILE_RESULT TRUE)
 if (CMAKE_PROJECT_NAME STREQUAL Urho3D AND TARGET Urho3D)
     # A special case where library location is already known to be in the build tree of Urho3D project
     set (URHO3D_HOME ${CMAKE_BINARY_DIR})
@@ -59,15 +59,24 @@ if (CMAKE_PROJECT_NAME STREQUAL Urho3D AND TARGET Urho3D)
     endif ()
     set (URHO3D_LIBRARIES Urho3D)
     set (FOUND_MESSAGE "Found Urho3D: as CMake target")
+    set (URHO3D_COMPILE_RESULT TRUE)
 else ()
     # Library location would be searched (based on URHO3D_HOME variable if provided and in system-wide default location)
     if (NOT URHO3D_HOME AND DEFINED ENV{URHO3D_HOME})
         file (TO_CMAKE_PATH "$ENV{URHO3D_HOME}" URHO3D_HOME)
     endif ()
-    # If either of the URHO3D_LIB_TYPE or URHO3D_HOME build options changes then invalidate all the caches
-    if ((URHO3D_LIB_TYPE AND NOT URHO3D_LIB_TYPE STREQUAL URHO3D_FOUND_LIB_TYPE) OR NOT URHO3D_BASE_INCLUDE_DIR MATCHES "^${URHO3D_HOME}/include/Urho3D$")
+    # If either of the URHO3D_64BIT or URHO3D_LIB_TYPE or URHO3D_HOME build options changes then invalidate all the caches
+    if (URHO3D_64BIT)
+        set (URHO3D_64BIT 1)    # Convert to integer literal to match it with our internal cache representation
+    else ()
+        set (URHO3D_64BIT 0)
+    endif ()
+    if (NOT URHO3D_64BIT EQUAL URHO3D_FOUND_64BIT OR NOT URHO3D_LIB_TYPE STREQUAL URHO3D_FOUND_LIB_TYPE OR NOT URHO3D_BASE_INCLUDE_DIR MATCHES "^${URHO3D_HOME}/include/Urho3D$")
         unset (URHO3D_BASE_INCLUDE_DIR CACHE)
         unset (URHO3D_LIBRARIES CACHE)
+        unset (URHO3D_FOUND_64BIT CACHE)
+        unset (URHO3D_FOUND_LIB_TYPE CACHE)
+        unset (URHO3D_COMPILE_RESULT CACHE)
         if (WIN32)
             unset (URHO3D_LIBRARIES_DBG CACHE)
             unset (URHO3D_DLL_REL CACHE)
@@ -112,21 +121,6 @@ else ()
         list (APPEND CMAKE_PREFIX_PATH ${URHO3D_HOME})
         set (SEARCH_OPT NO_CMAKE_FIND_ROOT_PATH)
     endif ()
-    # For Android platform, search in path similar to ANDROID_LIBRARY_OUTPUT_PATH variable
-    if (ANDROID)
-        if (URHO3D_HOME)
-            set (URHO3D_LIB_SEARCH_HINT HINTS ${URHO3D_HOME}/libs/${ANDROID_NDK_ABI_NAME})
-        else ()
-            set (URHO3D_LIB_SEARCH_HINT HINTS /usr/local/libs/${ANDROID_NDK_ABI_NAME})
-        endif ()
-    endif ()
-    if (NOT URHO3D_64BIT)
-        # For 32-bit, force to search in 'lib' path even when the host system defaulted to use 'lib64'
-        set_property (GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS FALSE)
-    elseif (MINGW AND CMAKE_CROSSCOMPILING)
-        # For 64-bit MinGW on Linux host system, force to search in 'lib64' path even when the Windows platform is not defaulted to use it
-        set_property (GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS TRUE)
-    endif ()
     find_path (URHO3D_BASE_INCLUDE_DIR Urho3D.h PATH_SUFFIXES ${PATH_SUFFIX} ${SEARCH_OPT} DOC "Urho3D include directory")
     if (URHO3D_BASE_INCLUDE_DIR)
         get_filename_component (URHO3D_INCLUDE_DIRS ${URHO3D_BASE_INCLUDE_DIR} PATH)
@@ -149,73 +143,142 @@ else ()
             file (STRINGS ${URHO3D_BASE_INCLUDE_DIR}/Urho3D.h MSVC_STATIC_LIB REGEX "^#define URHO3D_STATIC_DEFINE$")
         endif ()
     endif ()
-    find_library (URHO3D_LIBRARIES NAMES Urho3D ${URHO3D_LIB_SEARCH_HINT} PATH_SUFFIXES ${PATH_SUFFIX} ${SEARCH_OPT} DOC "Urho3D library directory")
-    if (WIN32)
-        # For Windows platform, give a second chance to search for a debug version of the library
-        find_library (URHO3D_LIBRARIES_DBG NAMES Urho3D_d ${URHO3D_LIB_SEARCH_HINT} PATH_SUFFIXES ${PATH_SUFFIX} ${SEARCH_OPT})
-        # If both the non-debug and debug version of the libraries are found then use them both
-        set (URHO3D_LIBRARIES_REL ${URHO3D_LIBRARIES})
-        # Otherwise, URHO3D_LIBRARIES variable should have the path to either one of the version
-        if (URHO3D_LIBRARIES)
-            if (URHO3D_LIBRARIES_DBG)
-                list (APPEND URHO3D_LIBRARIES ${URHO3D_LIBRARIES_DBG})
-            endif ()
+    # For Android platform, search in path similar to ANDROID_LIBRARY_OUTPUT_PATH variable
+    if (ANDROID)
+        if (URHO3D_HOME)
+            set (URHO3D_LIB_SEARCH_HINT HINTS ${URHO3D_HOME}/libs/${ANDROID_NDK_ABI_NAME})
         else ()
-            set (URHO3D_LIBRARIES ${URHO3D_LIBRARIES_DBG})
+            set (URHO3D_LIB_SEARCH_HINT HINTS /usr/local/libs/${ANDROID_NDK_ABI_NAME})
         endif ()
     endif ()
-    # Ensure the module has found the right one if the library type is specified
-    if (MSVC)
-        if (URHO3D_LIB_TYPE)
-            if (NOT ((URHO3D_LIB_TYPE STREQUAL STATIC AND MSVC_STATIC_LIB) OR (URHO3D_LIB_TYPE STREQUAL SHARED AND NOT MSVC_STATIC_LIB)))
-                unset (URHO3D_LIB_TYPE)
+    if (URHO3D_64BIT AND MINGW AND CMAKE_CROSSCOMPILING)
+        # For 64-bit MinGW on Linux host system, force to search in 'lib64' path even when the Windows platform is not defaulted to use it
+        set_property (GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS TRUE)
+    endif ()
+    if (NOT URHO3D_LIB_TYPE)
+        set (URHO3D_LIB_TYPE_WAS_UNDEFINED TRUE)    # We need this to undefine the auto-discovered URHO3D_LIB_TYPE variable before looping
+    endif ()
+    foreach (ABI_64BIT RANGE ${URHO3D_64BIT} 0)
+        # Break if the compiler is not multilib-capable and the ABI is not its native
+        if ((MSVC OR MINGW OR ANDROID OR RPI OR EMSCRIPTEN) AND NOT ABI_64BIT EQUAL URHO3D_DEFAULT_64BIT)
+            break ()
+        endif ()
+        # Set to search in 'lib' or 'lib64' based on the ABI being tested
+        set_property (GLOBAL PROPERTY FIND_LIBRARY_USE_LIB64_PATHS ${ABI_64BIT})
+        find_library (URHO3D_LIBRARIES NAMES Urho3D ${URHO3D_LIB_SEARCH_HINT} PATH_SUFFIXES ${PATH_SUFFIX} ${SEARCH_OPT} DOC "Urho3D library directory")
+        if (WIN32)
+            # For Windows platform, give a second chance to search for a debug version of the library
+            find_library (URHO3D_LIBRARIES_DBG NAMES Urho3D_d ${URHO3D_LIB_SEARCH_HINT} PATH_SUFFIXES ${PATH_SUFFIX} ${SEARCH_OPT})
+            # If both the non-debug and debug version of the libraries are found then use them both
+            set (URHO3D_LIBRARIES_REL ${URHO3D_LIBRARIES})
+            # Otherwise, URHO3D_LIBRARIES variable should have the path to either one of the version
+            if (URHO3D_LIBRARIES)
+                if (URHO3D_LIBRARIES_DBG)
+                    list (APPEND URHO3D_LIBRARIES ${URHO3D_LIBRARIES_DBG})
+                endif ()
+            else ()
+                set (URHO3D_LIBRARIES ${URHO3D_LIBRARIES_DBG})
             endif ()
-        else ()
-            if (MSVC_STATIC_LIB)
+        endif ()
+        # Ensure the module has found the right one if the library type is specified
+        if (MSVC)
+            if (URHO3D_LIB_TYPE)
+                if (NOT ((URHO3D_LIB_TYPE STREQUAL STATIC AND MSVC_STATIC_LIB) OR (URHO3D_LIB_TYPE STREQUAL SHARED AND NOT MSVC_STATIC_LIB)))
+                    unset (URHO3D_LIBRARIES)    # Not a right type, so nullify the search result
+                endif ()
+            else ()
+                if (MSVC_STATIC_LIB)
+                    set (URHO3D_LIB_TYPE STATIC)
+                else ()
+                    set (URHO3D_LIB_TYPE SHARED)
+                endif ()
+            endif ()
+        elseif (URHO3D_LIBRARIES)
+            get_filename_component (EXT ${URHO3D_LIBRARIES} EXT)
+            if (EXT STREQUAL .a)
                 set (URHO3D_LIB_TYPE STATIC)
+                # For Non-MSVC compiler the static define is not baked into the export header file so we need to define it for the try_run below
+                set (COMPILER_STATIC_FLAG COMPILE_DEFINITIONS -DURHO3D_STATIC_DEFINE)
             else ()
                 set (URHO3D_LIB_TYPE SHARED)
+                unset (COMPILER_STATIC_FLAG)
             endif ()
         endif ()
-    elseif (URHO3D_LIBRARIES)
-        get_filename_component (EXT ${URHO3D_LIBRARIES} EXT)
-        if (EXT STREQUAL .a)
-            set (URHO3D_LIB_TYPE STATIC)
-            # For Non-MSVC compiler the static define is not baked into the export header file so we need to define it for the try_compile below
-            set (COMPILER_STATIC_FLAG COMPILE_DEFINITIONS -DURHO3D_STATIC_DEFINE)
-        else ()
-            set (URHO3D_LIB_TYPE SHARED)
+        # For shared library type, also initialize the URHO3D_DLL variable for later use
+        if (WIN32)
+            if (URHO3D_LIB_TYPE STREQUAL SHARED AND URHO3D_HOME)
+                find_file (URHO3D_DLL_REL Urho3D.dll HINTS ${URHO3D_HOME}/bin NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH DOC "Urho3D release DLL")
+                if (URHO3D_DLL_REL)
+                    list (APPEND URHO3D_DLL ${URHO3D_DLL_REL})
+                endif ()
+                find_file (URHO3D_DLL_DBG Urho3D_d.dll HINTS ${URHO3D_HOME}/bin NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH DOC "Urho3D debug DLL")
+                if (URHO3D_DLL_DBG)
+                    list (APPEND URHO3D_DLL ${URHO3D_DLL_DBG})
+                endif ()
+            endif ()
         endif ()
-    endif ()
-    # Ensure the module has found the library with the right ABI for the chosen compiler and URHO3D_64BIT build option
-    if (URHO3D_LIBRARIES)
-        if (NOT URHO3D_64BIT AND NOT MSVC AND NOT MINGW AND NOT ANDROID AND NOT RPI AND NOT EMSCRIPTEN)
-            set (COMPILER_32BIT_FLAG -DCOMPILE_DEFINITIONS:STRING=-m32)
-        endif ()
-        if (IOS)
-            set (CMAKE_OSX_SYSROOT iphoneos)   # Since this is anyway the correct sysroot setting for IOS platform, we do not revert the setting back after we are done
-            set (IOS_FLAGS -DCMAKE_MACOSX_BUNDLE=1 -DCMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_REQUIRED=0)
-        elseif (ANDROID)
-            set (ANDROID_LIBRARIES "log\;android\;GLESv1_CM\;GLESv2")
-        endif ()
-        # BCM_VC_LIBRARIES variable is already set by FindBCM_VC module on RPI platform
-        try_compile (COMPILE_RESULT ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_LIST_DIR}/CheckUrho3DLibrary.cpp
-            CMAKE_FLAGS ${IOS_FLAGS} -DLINK_LIBRARIES:STRING=${URHO3D_LIBRARIES}\;${BCM_VC_LIBRARIES}\;${ANDROID_LIBRARIES} -DINCLUDE_DIRECTORIES:STRING=${URHO3D_INCLUDE_DIRS} ${COMPILER_32BIT_FLAG} ${COMPILER_STATIC_FLAG}
-            OUTPUT_VARIABLE TRY_COMPILE_OUT)
-    endif ()
-    # For shared library type, also initialize the URHO3D_DLL variable for later use
-    if (WIN32)
-        if (URHO3D_LIB_TYPE STREQUAL SHARED AND URHO3D_HOME)
-            find_file (URHO3D_DLL_REL Urho3D.dll HINTS ${URHO3D_HOME}/bin NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH DOC "Urho3D release DLL")
-            if (URHO3D_DLL_REL)
-                list (APPEND URHO3D_DLL ${URHO3D_DLL_REL})
+        # Ensure the module has found the library with the right ABI for the chosen compiler and URHO3D_64BIT build option (if specified)
+        if (URHO3D_LIBRARIES AND NOT URHO3D_COMPILE_RESULT)
+            if (NOT (MSVC OR MINGW OR ANDROID OR RPI OR EMSCRIPTEN) AND NOT ABI_64BIT)
+                set (COMPILER_32BIT_FLAG -DCOMPILE_DEFINITIONS:STRING=-m32)
             endif ()
-            find_file (URHO3D_DLL_DBG Urho3D_d.dll HINTS ${URHO3D_HOME}/bin NO_DEFAULT_PATH NO_CMAKE_FIND_ROOT_PATH DOC "Urho3D debug DLL")
-            if (URHO3D_DLL_DBG)
-                list (APPEND URHO3D_DLL ${URHO3D_DLL_DBG})
+            # Below variables are loop invariant but there is no harm to keep them here
+            if (IOS)
+                set (IOS_FLAGS -DCMAKE_MACOSX_BUNDLE=1 -DCMAKE_XCODE_ATTRIBUTE_CODE_SIGNING_REQUIRED=0)
+            elseif (ANDROID)
+                set (ANDROID_LIBRARIES "log\;android\;GLESv1_CM\;GLESv2")
+            elseif (MSVC AND URHO3D_DLL)
+                # This is a hack as it relies on internal implementation of try_run
+                foreach (DLL ${URHO3D_DLL})
+                    get_filename_component (NAME ${DLL} NAME)
+                    execute_process (COMMAND ${CMAKE_COMMAND} -E copy ${DLL} ${CMAKE_BINARY_DIR}/CMakeFiles/CMakeTmp/Debug/${NAME})
+                endforeach ()
+            endif ()
+            # Since in cross-compiling mode we cannot run the test target executable and auto-discover the build options used by the found library,
+            # the next best thing is evaluate the found export header indirectly (assuming the found library was built using the same export header)
+            if (CMAKE_CROSSCOMPILING)
+                set (URHO3D_RUN_RESULT 0)
+                file (READ ${URHO3D_BASE_INCLUDE_DIR}/Urho3D.h URHO3D_RUN_RESULT__TRYRUN_OUTPUT)
+            endif ()
+            # BCM_VC_LIBRARIES variable is already set by FindBCM_VC module on RPI platform
+            try_run (URHO3D_RUN_RESULT URHO3D_COMPILE_RESULT ${CMAKE_BINARY_DIR} ${CMAKE_CURRENT_LIST_DIR}/CheckUrho3DLibrary.cpp
+                CMAKE_FLAGS ${IOS_FLAGS} -DLINK_LIBRARIES:STRING=${URHO3D_LIBRARIES}\;${BCM_VC_LIBRARIES}\;${ANDROID_LIBRARIES} -DINCLUDE_DIRECTORIES:STRING=${URHO3D_INCLUDE_DIRS} ${COMPILER_32BIT_FLAG} ${COMPILER_STATIC_FLAG}
+                COMPILE_OUTPUT_VARIABLE TRY_COMPILE_OUT RUN_OUTPUT_VARIABLE TRY_RUN_OUT)
+            set (URHO3D_COMPILE_RESULT ${URHO3D_COMPILE_RESULT} CACHE INTERNAL "FindUrho3D module's compile result")
+            if (URHO3D_COMPILE_RESULT AND URHO3D_RUN_RESULT EQUAL 0)
+                # Auto-discover build options used by the found library
+                set (URHO3D_64BIT ${ABI_64BIT} CACHE BOOL "Enable 64-bit build, the value is auto-discovered based on the found Urho3D library" FORCE) # Force it as it is more authoritative than user-specified option
+                set (URHO3D_LIB_TYPE ${URHO3D_LIB_TYPE} CACHE BOOL "Urho3D library type, the value is auto-discovered based on the found Urho3D library" FORCE) # Use the Force, Luke
+                if (TRY_RUN_OUT MATCHES "#define URHO3D_OPENGL")
+                    set (URHO3D_OPENGL 1 CACHE INTERNAL "Auto-discovered URHO3D_OPENGL build option" FORCE)
+                    unset (URHO3D_D3D11 CACHE)
+                    unset (URHO3D_D3D11)
+                else ()
+                    if (TRY_RUN_OUT MATCHES "#define URHO3D_D3D11")
+                        set (URHO3D_D3D11 1 CACHE INTERNAL "Auto-discovered URHO3D_D3D11 build option" FORCE)
+                    else ()
+                        unset (URHO3D_D3D11 CACHE)
+                        unset (URHO3D_D3D11)
+                    endif ()
+                endif ()
+                if (TRY_RUN_OUT MATCHES "#define URHO3D_SSE")
+                    set (AUTO_DISCOVERED_SSE 1)
+                endif ()
+                set (URHO3D_SSE ${AUTO_DISCOVERED_SSE} CACHE INTERNAL "Auto-discovered URHO3D_SSE build option" FORCE)
+                break ()
+            else ()
+                # Prepare for the second attempt by resetting the variables as necessary
+                if (URHO3D_LIB_TYPE_WAS_UNDEFINED)
+                    unset (URHO3D_LIB_TYPE)
+                endif ()
+                unset (URHO3D_LIBRARIES CACHE)
+                if (WIN32)
+                    unset (URHO3D_LIBRARIES_DBG CACHE)
+                endif ()
+                unset (URHO3D_DLL)
             endif ()
         endif ()
-    endif ()
+    endforeach ()
     # Restore CMake global settings
     if (CMAKE_FIND_LIBRARY_SUFFIXES_SAVED)
         set (CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES_SAVED})
@@ -225,7 +288,7 @@ else ()
     endif ()
 endif ()
 
-if (URHO3D_INCLUDE_DIRS AND URHO3D_LIBRARIES AND URHO3D_LIB_TYPE AND COMPILE_RESULT)
+if (URHO3D_INCLUDE_DIRS AND URHO3D_LIBRARIES AND URHO3D_LIB_TYPE AND URHO3D_COMPILE_RESULT)
     set (URHO3D_FOUND 1)
     if (NOT FOUND_MESSAGE)
         set (FOUND_MESSAGE "Found Urho3D: ${URHO3D_LIBRARIES}")
@@ -236,6 +299,7 @@ if (URHO3D_INCLUDE_DIRS AND URHO3D_LIBRARIES AND URHO3D_LIB_TYPE AND COMPILE_RES
     include (FindPackageMessage)
     find_package_message (Urho3D ${FOUND_MESSAGE} "[${URHO3D_LIBRARIES}][${URHO3D_INCLUDE_DIRS}]")
     set (URHO3D_HOME ${URHO3D_HOME} CACHE PATH "Path to Urho3D build tree or SDK installation location" FORCE)
+    set (URHO3D_FOUND_64BIT ${URHO3D_64BIT} CACHE INTERNAL "True when 64-bit ABI was being used when test compiling Urho3D library")
     set (URHO3D_FOUND_LIB_TYPE ${URHO3D_LIB_TYPE} CACHE INTERNAL "Lib type (if specified) when Urho3D library was last found")
 elseif (Urho3D_FIND_REQUIRED)
     if (ANDROID)

+ 45 - 41
CMake/Modules/Urho3D-CMake-common.cmake

@@ -41,6 +41,29 @@ if (CMAKE_GENERATOR STREQUAL Xcode)
     set (XCODE TRUE)
 endif ()
 
+# Rightfully we could have performed this inside a CMake/iOS toolchain file but we don't have one nor need for one
+if (IOS)
+    set (CMAKE_CROSSCOMPILING TRUE)
+    set (CMAKE_XCODE_EFFECTIVE_PLATFORMS -iphoneos -iphonesimulator)
+    set (CMAKE_OSX_SYSROOT iphoneos)    # Set Base SDK to "Latest iOS"
+    if (NOT IOS_SYSROOT)
+        execute_process (COMMAND xcodebuild -version -sdk ${CMAKE_OSX_SYSROOT} Path OUTPUT_VARIABLE IOS_SYSROOT OUTPUT_STRIP_TRAILING_WHITESPACE)   # Obtain iOS sysroot path
+        set (IOS_SYSROOT ${IOS_SYSROOT} CACHE INTERNAL "Path to iOS system root")
+    endif ()
+    set (CMAKE_FIND_ROOT_PATH ${IOS_SYSROOT})
+    # Ensure the CMAKE_OSX_DEPLOYMENT_TARGET is set to empty
+    set (CMAKE_OSX_DEPLOYMENT_TARGET)
+    unset (CMAKE_OSX_DEPLOYMENT_TARGET CACHE)
+elseif (XCODE)
+    set (CMAKE_OSX_SYSROOT macosx)    # Set Base SDK to "Latest OS X"
+    if (NOT CMAKE_OSX_DEPLOYMENT_TARGET)
+        # If not set, set to current running build system OS version by default
+        execute_process (COMMAND sw_vers -productVersion OUTPUT_VARIABLE CURRENT_OSX_VERSION OUTPUT_STRIP_TRAILING_WHITESPACE)
+        string (REGEX REPLACE ^\([^.]+\\.[^.]+\).* \\1 CMAKE_OSX_DEPLOYMENT_TARGET ${CURRENT_OSX_VERSION})
+        set (CMAKE_OSX_DEPLOYMENT_TARGET ${CMAKE_OSX_DEPLOYMENT_TARGET} CACHE INTERNAL "OSX deployment target")
+    endif ()
+endif ()
+
 # Define all supported build options
 include (CMakeDependentOption)
 option (URHO3D_C++11 "Enable C++11 standard")
@@ -50,18 +73,18 @@ if (NOT DEFINED URHO3D_DEFAULT_64BIT)  # Only do this once in the initial config
     if (MSVC)
         # On MSVC compiler, use the chosen CMake/VS generator to determine the ABI
         if (CMAKE_GENERATOR MATCHES Win64)
-            set (URHO3D_DEFAULT_64BIT TRUE)
+            set (URHO3D_DEFAULT_64BIT 1)
         else ()
-            set (URHO3D_DEFAULT_64BIT FALSE)
+            set (URHO3D_DEFAULT_64BIT 0)
         endif ()
     else ()
         # On non-MSVC compiler, default to build 64-bit when the chosen compiler toolchain in the build tree has a 64-bit build environment
         execute_process (COMMAND ${CMAKE_COMMAND} -E echo COMMAND ${CMAKE_C_COMPILER} -E -dM - OUTPUT_VARIABLE PREDEFINED_MACROS ERROR_QUIET)
         string (REGEX MATCH "#define +__(x86_64|aarch64)__ +1" matched "${PREDEFINED_MACROS}")
         if (matched)
-            set (URHO3D_DEFAULT_64BIT TRUE)
+            set (URHO3D_DEFAULT_64BIT 1)
         else ()
-            set (URHO3D_DEFAULT_64BIT FALSE)
+            set (URHO3D_DEFAULT_64BIT 0)
         endif ()
         # The 'ANDROID' CMake variable is already set by android.toolchain.cmake when it is being used for cross-compiling Android
         # When ANDROID is true and ARM is not then we are targeting Android on Intel Atom
@@ -147,13 +170,6 @@ if (CMAKE_HOST_WIN32)
 else ()
     set (NULL_DEVICE /dev/null)
 endif ()
-# Find Direct3D include & library directories in MS Windows SDK or DirectX SDK when not using OpenGL.
-if (WIN32 AND NOT URHO3D_OPENGL)
-    find_package (Direct3D REQUIRED)
-    if (DIRECT3D_INCLUDE_DIRS)
-        include_directories (${DIRECT3D_INCLUDE_DIRS})
-    endif ()
-endif ()
 # For Raspbery Pi, find Broadcom VideoCore IV firmware
 if (RPI)
     find_package (BCM_VC REQUIRED)
@@ -189,7 +205,7 @@ if (CMAKE_PROJECT_NAME STREQUAL Urho3D)
 else ()
     set (URHO3D_LIB_TYPE "" CACHE STRING "Specify Urho3D library type, possible values are STATIC and SHARED")
     set (URHO3D_HOME "" CACHE PATH "Path to Urho3D build tree or SDK installation location (downstream project only)")
-    if (URHO3D_PCH OR URHO3D_UPDATE_SOURCE_TREE OR URHO3D_TOOLS OR URHO3D_EXTRAS)
+    if (URHO3D_PCH OR URHO3D_UPDATE_SOURCE_TREE OR URHO3D_TOOLS)
         # Just reference it to suppress "unused variable" CMake warning on downstream projects using this CMake module
     endif ()
     # All Urho3D downstream projects require Urho3D library, so find Urho3D library here now
@@ -225,7 +241,7 @@ endif ()
 cmake_dependent_option (URHO3D_STATIC_RUNTIME "Use static C/C++ runtime libraries and eliminate the need for runtime DLLs installation (VS only)" FALSE "MSVC" FALSE)
 cmake_dependent_option (URHO3D_WIN32_CONSOLE "Use console main() as entry point when setting up Windows executable targets (Windows platform only)" FALSE "WIN32" FALSE)
 cmake_dependent_option (URHO3D_MACOSX_BUNDLE "Use MACOSX_BUNDLE when setting up Mac OS X executable targets (Xcode native build only)" FALSE "XCODE AND NOT IOS" FALSE)
-if (CMAKE_CROSSCOMPILING AND NOT ANDROID)
+if (CMAKE_CROSSCOMPILING AND NOT ANDROID AND NOT IOS)
     set (URHO3D_SCP_TO_TARGET "" CACHE STRING "Use scp to transfer executables to target system (non-Android cross-compiling build only), SSH digital key must be setup first for this to work, typical value has a pattern of usr@tgt:remote-loc")
 else ()
     unset (URHO3D_SCP_TO_TARGET CACHE)
@@ -335,11 +351,6 @@ if ($ENV{COVERITY_SCAN_BRANCH})
     add_definitions (-DCOVERITY_SCAN_MODEL)
 endif ()
 
-# Enable SSE instruction set. Requires Pentium III or Athlon XP processor at minimum.
-if (URHO3D_SSE)
-    add_definitions (-DURHO3D_SSE)
-endif ()
-
 # Enable NEON instruction set.
 if (URHO3D_NEON)
     add_definitions (-DURHO3D_NEON -DSTBI_NEON)     # BT_USE_NEON is already being self-defined by Bullet library as appropriate
@@ -393,15 +404,9 @@ if (EMSCRIPTEN)
     add_definitions (-DNO_POPEN)
 endif ()
 
-# Add definition for Direct3D11
+# URHO3D_D3D11 overrides URHO3D_OPENGL option
 if (URHO3D_D3D11)
     set (URHO3D_OPENGL 0)
-    add_definitions (-DURHO3D_D3D11)
-endif ()
-
-# Add definition for OpenGL
-if (URHO3D_OPENGL)
-    add_definitions (-DURHO3D_OPENGL)
 endif ()
 
 # Add definitions for GLEW
@@ -415,7 +420,10 @@ if (URHO3D_LIB_TYPE)
 endif ()
 if (NOT URHO3D_LIB_TYPE STREQUAL SHARED)
     set (URHO3D_LIB_TYPE STATIC)
-    if (NOT MSVC)   # This define will be baked into the export header for MSVC compiler
+    if (MSVC)
+        # This define will be baked into the export header for MSVC compiler
+        set (URHO3D_STATIC_DEFINE 1)
+    else ()
         add_definitions (-DURHO3D_STATIC_DEFINE)
     endif ()
 endif ()
@@ -476,6 +484,14 @@ if (URHO3D_DATABASE_SQLITE)
     add_definitions (-DURHO3D_DATABASE -DURHO3D_DATABASE_SQLITE)
 endif ()
 
+# Find Direct3D include & library directories in MS Windows SDK or DirectX SDK when not using OpenGL.
+if (WIN32 AND NOT URHO3D_OPENGL)
+    find_package (Direct3D REQUIRED)
+    if (DIRECT3D_INCLUDE_DIRS)
+        include_directories (${DIRECT3D_INCLUDE_DIRS})
+    endif ()
+endif ()
+
 # Platform and compiler specific options
 if (URHO3D_C++11)
     add_definitions (-DURHO3D_CXX11)   # Note the define is NOT 'URHO3D_C++11'!
@@ -513,26 +529,14 @@ if (IOS)
     else ()
         set (CMAKE_OSX_ARCHITECTURES $(ARCHS_STANDARD_32_BIT))
     endif ()
-    set (CMAKE_XCODE_EFFECTIVE_PLATFORMS -iphoneos -iphonesimulator)
-    set (CMAKE_OSX_SYSROOT iphoneos)    # Set Base SDK to "Latest iOS"
-    execute_process (COMMAND xcodebuild -version -sdk ${CMAKE_OSX_SYSROOT} Path OUTPUT_VARIABLE IOS_SYSROOT OUTPUT_STRIP_TRAILING_WHITESPACE)   # Obtain iOS sysroot path
-    set (CMAKE_FIND_ROOT_PATH ${IOS_SYSROOT})
-    # Ensure the CMAKE_OSX_DEPLOYMENT_TARGET is set to empty
-    unset (CMAKE_OSX_DEPLOYMENT_TARGET CACHE)
 elseif (XCODE)
-    # MacOSX-Xcode-specific setup
+    # OSX-specific setup
     if (NOT URHO3D_64BIT)
         set (CMAKE_OSX_ARCHITECTURES $(ARCHS_STANDARD_32_BIT))
     endif ()
-    set (CMAKE_OSX_SYSROOT macosx)    # Set Base SDK to "Latest OS X"
-    if (NOT CMAKE_OSX_DEPLOYMENT_TARGET)
-        # If not set, set to current running build system OS version by default
-        execute_process (COMMAND sw_vers -productVersion OUTPUT_VARIABLE CURRENT_OSX_VERSION OUTPUT_STRIP_TRAILING_WHITESPACE)
-        string (REGEX REPLACE ^\([^.]+\\.[^.]+\).* \\1 CMAKE_OSX_DEPLOYMENT_TARGET ${CURRENT_OSX_VERSION})
-    endif ()
 endif ()
 if (IOS OR URHO3D_MACOSX_BUNDLE)
-    # Common MacOSX and iOS bundle setup
+    # Common OSX and iOS bundle setup
     if (NOT MACOSX_BUNDLE_GUI_IDENTIFIER)
         set (MACOSX_BUNDLE_GUI_IDENTIFIER com.github.urho3d.\${PRODUCT_NAME:bundleIdentifier:lower})
     endif ()
@@ -541,7 +545,7 @@ if (IOS OR URHO3D_MACOSX_BUNDLE)
     endif ()
 endif ()
 if (MSVC)
-    # Visual Studio-specific setup
+    # VS-specific setup
     add_definitions (-D_CRT_SECURE_NO_WARNINGS)
     # Note: All CMAKE_xxx_FLAGS variables are not in list context (although they should be)
     set (CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} ${DEBUG_RUNTIME}")

+ 7 - 0
CMake/Modules/exportheader.cmake.in

@@ -23,6 +23,11 @@
 #ifndef @INCLUDE_GUARD_NAME@
 #define @INCLUDE_GUARD_NAME@
 @PRE_EXPORT_HEADER@
+#cmakedefine URHO3D_STATIC_DEFINE
+#cmakedefine URHO3D_OPENGL
+#cmakedefine URHO3D_D3D11
+#cmakedefine URHO3D_SSE
+
 #ifdef @STATIC_DEFINE@
 #  define @EXPORT_MACRO_NAME@
 #  define @NO_EXPORT_MACRO_NAME@
@@ -58,5 +63,7 @@
 #if DEFINE_NO_DEPRECATED
 # define @NO_DEPRECATED_MACRO_NAME@
 #endif
+
+#define NONSCRIPTABLE @ANNOTATE_NONSCRIPTABLE@
 @POST_EXPORT_HEADER@
 #endif

+ 1 - 1
Source/ThirdParty/LuaJIT/CMakeLists.txt

@@ -316,7 +316,7 @@ set (DASM_FLAGS ${DASM_XFLAGS} ${DASM_AFLAGS})
 
 # Makefile: Make targets
 # The host tool must be built natively
-if (CMAKE_CROSSCOMPILING OR IOS)
+if (CMAKE_CROSSCOMPILING)
     # Escape the variables
     foreach (ESCAPED_VAR HOST_XCFLAGS TARGET_ARCH DASM_FLAGS DASM_ARCH)
         string (REPLACE -D +D ${ESCAPED_VAR} "${${ESCAPED_VAR}}")

+ 2 - 2
Source/Tools/CMakeLists.txt

@@ -33,7 +33,7 @@ if (NOT EMSCRIPTEN)
 endif ()
 
 # Build PackageTool using host compiler toolchain
-if ((CMAKE_CROSSCOMPILING OR IOS) AND URHO3D_PACKAGING)
+if (CMAKE_CROSSCOMPILING AND URHO3D_PACKAGING)
     # When cross-compiling, build the host tool as external project
     include (ExternalProject)
     if (IOS)
@@ -62,7 +62,7 @@ if (URHO3D_TOOLS)
     if (URHO3D_ANGELSCRIPT)
         add_subdirectory (ScriptCompiler)
     endif ()
-elseif ((NOT CMAKE_CROSSCOMPILING AND NOT IOS) AND URHO3D_PACKAGING)
+elseif (NOT CMAKE_CROSSCOMPILING AND URHO3D_PACKAGING)
     # PackageTool target is required but we are not cross-compiling, so build it as per normal
     add_subdirectory (PackageTool)
 endif ()

+ 4 - 9
Source/Urho3D/CMakeLists.txt

@@ -131,7 +131,7 @@ if (URHO3D_BINDINGS)
 endif ()
 if (URHO3D_LUA)
     # The host tool must be built natively
-    if (CMAKE_CROSSCOMPILING OR IOS)
+    if (CMAKE_CROSSCOMPILING)
         # When cross-compiling, build the host tool as external project
         include (ExternalProject)
         if (IOS)
@@ -169,15 +169,10 @@ endif ()
 
 # Generate platform specific export header file
 if (MSVC)
-    if (URHO3D_LIB_TYPE STREQUAL STATIC)
-        set (BAKED_IN_DEFINE "\n#define URHO3D_STATIC_DEFINE\n")
-    endif ()
-    set (PRE_EXPORT_HEADER "\n#pragma warning(disable: 4251)\n#pragma warning(disable: 4275)\n${BAKED_IN_DEFINE}")
+    set (PRE_EXPORT_HEADER "\n#pragma warning(disable: 4251)\n#pragma warning(disable: 4275)\n")
 endif ()
 if (URHO3D_CLANG_TOOLS)
-    set (POST_EXPORT_HEADER "\n#define NONSCRIPTABLE __attribute__((annotate(\"nonscriptable\")))\n")
-else ()
-    set (POST_EXPORT_HEADER "\n#define NONSCRIPTABLE\n")
+    set (ANNOTATE_NONSCRIPTABLE "__attribute__((annotate(\"nonscriptable\")))")
 endif ()
 generate_export_header (${TARGET_NAME} ${URHO3D_LIB_TYPE} EXPORT_MACRO_NAME URHO3D_API EXPORT_FILE_NAME Urho3D.h.new)
 execute_process (COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_BINARY_DIR}/Urho3D.h.new ${CMAKE_CURRENT_BINARY_DIR}/Urho3D.h)
@@ -387,7 +382,7 @@ 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)
 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
+    if (NOT MSVC AND NOT (MINGW AND CMAKE_CROSSCOMPILING) 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 ()