ソースを参照

For downstream - auto detect Urho3D lib type from build artifact.

Yao Wei Tjong 姚伟忠 10 年 前
コミット
13e4cc332e
3 ファイル変更80 行追加40 行削除
  1. 75 38
      CMake/Modules/FindUrho3D.cmake
  2. 1 1
      Docs/GettingStarted.dox
  3. 4 1
      Source/Urho3D/CMakeLists.txt

+ 75 - 38
CMake/Modules/FindUrho3D.cmake

@@ -20,7 +20,13 @@
 # THE SOFTWARE.
 #
 
-# Find Urho3D include directories and library in source & build tree or installed Urho3D SDK.
+# Find Urho3D include directories and libraries in the Urho3D SDK installation or build tree
+# This module should be able to find Urho3D automatically when the SDK is installed in a system-wide default location
+# If the SDK installation location is non-default or the Urho3D library is not installed at all (i.e. still in its build tree) then
+#   use URHO3D_HOME environment variable or build option to specify the location of the non-default SDK installation or build tree
+# When setting URHO3D_HOME variable, it should be set to a parent directory containing both the "include" and "lib" subdirectories
+#   e.g. set URHO3D_HOME=/home/john/usr/local, if the SDK is installed using DESTDIR=/home/john and CMAKE_INSTALL_PREFIX=/usr/local
+
 #
 #  URHO3D_FOUND
 #  URHO3D_INCLUDE_DIRS
@@ -35,42 +41,9 @@
 #  URHO3D_DLL_DBG
 #
 
-# For internal Urho3D project, the Urho3D "build tree" path is already known.
-#
-# For external project that attempts to use the Urho3D build tree or installed Urho3D SDK,
-# use URHO3D_HOME environment variable or build option to specify the path (not needed when the path is a system-wide default location).
-# When setting URHO3D_HOME variable, it should be set to a parent directory containing both the "include" or "lib" subdirectories.
-# For example: set URHO3D_HOME=/home/john/usr/local, if the SDK is installed using DESTDIR=/home/john and CMAKE_INSTALL_PREFIX=/usr/local
-#
-
-# 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
 set (PATH_SUFFIX Urho3D)
-if (NOT URHO3D_LIB_TYPE STREQUAL URHO3D_FOUND_LIB_TYPE OR (DEFINED URHO3D_BASE_INCLUDE_DIR AND NOT URHO3D_BASE_INCLUDE_DIR MATCHES "^${URHO3D_HOME}/include/${PATH_SUFFIX}"))
-    unset (URHO3D_BASE_INCLUDE_DIR CACHE)
-    unset (URHO3D_LIBRARIES CACHE)
-    if (WIN32)
-        unset (URHO3D_LIBRARIES_DBG CACHE)
-        unset (URHO3D_DLL_REL CACHE)
-        unset (URHO3D_DLL_DBG CACHE)
-    endif ()
-    set (URHO3D_FOUND_LIB_TYPE ${URHO3D_LIB_TYPE} CACHE INTERNAL "Lib type when Urho3D library was last found")
-    # Urho3D prefers static library type by default while CMake prefers shared one, so we need to change CMake preference to agree with Urho3D
-    if (NOT URHO3D_LIB_TYPE STREQUAL SHARED)
-        list (REVERSE CMAKE_FIND_LIBRARY_SUFFIXES)
-    endif ()
-    # Cater for the shared library extension in Emscripten build which is ".bc" instead of ".so"
-    if (EMSCRIPTEN)
-        string (REPLACE .so .bc CMAKE_FIND_LIBRARY_SUFFIXES "${CMAKE_FIND_LIBRARY_SUFFIXES}")   # Stringify for string replacement
-    endif ()
-endif ()
-
 if (CMAKE_PROJECT_NAME STREQUAL Urho3D AND TARGET Urho3D)
-    # A special case where library location is already known to be in the build tree
+    # A special case where library location is already known to be in the build tree of Urho3D project
     set (URHO3D_HOME ${CMAKE_BINARY_DIR})
     set (URHO3D_INCLUDE_DIRS ${URHO3D_HOME}/include ${URHO3D_HOME}/include/${PATH_SUFFIX}/ThirdParty)
     if (URHO3D_PHYSICS)
@@ -85,6 +58,42 @@ if (CMAKE_PROJECT_NAME STREQUAL Urho3D AND TARGET Urho3D)
     set (URHO3D_LIBRARIES Urho3D)
     set (FOUND_MESSAGE "Found Urho3D: as CMake target")
 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/${PATH_SUFFIX}$")
+        unset (URHO3D_BASE_INCLUDE_DIR CACHE)
+        unset (URHO3D_LIBRARIES CACHE)
+        if (WIN32)
+            unset (URHO3D_LIBRARIES_DBG CACHE)
+            unset (URHO3D_DLL_REL CACHE)
+            unset (URHO3D_DLL_DBG CACHE)
+        endif ()
+        # Urho3D prefers static library type by default while CMake prefers shared one, so we need to change CMake preference to agree with Urho3D
+        if (NOT CMAKE_FIND_LIBRARY_SUFFIXES MATCHES ^\\.\(a|lib\))
+            list (REVERSE CMAKE_FIND_LIBRARY_SUFFIXES)
+        endif ()
+        # If library type is specified then only search for the requested library type
+        if (NOT MSVC AND URHO3D_LIB_TYPE)      # MSVC static lib and import lib have a same extension, so cannot use it for searches
+            if (URHO3D_LIB_TYPE STREQUAL STATIC)
+                set (CMAKE_FIND_LIBRARY_SUFFIXES .a)
+            elseif (URHO3D_LIB_TYPE STREQUAL SHARED)
+                if (MINGW)
+                    set (CMAKE_FIND_LIBRARY_SUFFIXES .dll.a)
+                else ()
+                    set (CMAKE_FIND_LIBRARY_SUFFIXES .so)
+                endif ()
+            else ()
+                message (FATAL_ERROR "Library type: '${URHO3D_LIB_TYPE}' is not supported")
+            endif ()
+        endif ()
+        # Cater for the shared library extension in Emscripten build which is ".bc" instead of ".so"
+        if (EMSCRIPTEN)
+            string (REPLACE .so .bc CMAKE_FIND_LIBRARY_SUFFIXES "${CMAKE_FIND_LIBRARY_SUFFIXES}")   # Stringify for string replacement
+        endif ()
+    endif ()
     # URHO3D_HOME variable should be an absolute path, so use a non-rooted search even when we are cross-compiling
     if (URHO3D_HOME)
         list (APPEND CMAKE_PREFIX_PATH ${URHO3D_HOME})
@@ -137,6 +146,30 @@ else ()
         else ()
             set (URHO3D_LIBRARIES ${URHO3D_LIBRARIES_DBG})
         endif ()
+    endif ()
+    # Ensure the module has found the right one if the library type is specified
+    if (MSVC)
+        # The library type is baked into export header only for MSVC as it has no other way to differentiate them, i.e. both types cannot coexist on MSVC unlike other compilers
+        file (STRINGS "${URHO3D_BASE_INCLUDE_DIR}/Urho3D.h" MSVC_STATIC_LIB REGEX "^#define URHO3D_STATIC_DEFINE$")
+        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)
+            endif ()
+        else ()
+            if (MSVC_STATIC_LIB)
+                set (URHO3D_LIB_TYPE STATIC)
+            else ()
+                set (URHO3D_LIB_TYPE SHARED)
+            endif ()
+        endif ()
+    elseif (NOT URHO3D_LIB_TYPE)
+        if (URHO3D_LIBRARIES MATCHES \\.\(so|dll\\.a\)$)
+            set (URHO3D_LIB_TYPE SHARED)
+        elseif (URHO3D_LIBRARIES MATCHES \\.a$)
+            set (URHO3D_LIB_TYPE STATIC)
+        endif ()
+    endif ()
+    if (WIN32)
         # For shared library type, also initialize the URHO3D_DLL variable for later use
         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")
@@ -151,7 +184,7 @@ else ()
     endif ()
 endif ()
 
-if (URHO3D_INCLUDE_DIRS AND URHO3D_LIBRARIES)
+if (URHO3D_INCLUDE_DIRS AND URHO3D_LIBRARIES AND URHO3D_LIB_TYPE)
     set (URHO3D_FOUND 1)
     if (NOT FOUND_MESSAGE)
         set (FOUND_MESSAGE "Found Urho3D: ${URHO3D_LIBRARIES}")
@@ -162,13 +195,17 @@ if (URHO3D_INCLUDE_DIRS AND URHO3D_LIBRARIES)
     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_LIB_TYPE ${URHO3D_LIB_TYPE} CACHE INTERNAL "Lib type (if specified) when Urho3D library was last found")
 elseif (Urho3D_FIND_REQUIRED)
     if (ANDROID)
         set (NOT_FOUND_MESSAGE "For Android platform, double check if you have specified to use the same ANDROID_ABI as the Urho3D library in the build tree or SDK.")
     endif ()
+    if (URHO3D_LIB_TYPE)
+        set (NOT_FOUND_MESSAGE "Ensure the specified location contains the Urho3D library of the requested library type. ${NOT_FOUND_MESSAGE}")
+    endif ()
     message (FATAL_ERROR
-        "Could NOT find Urho3D library in Urho3D build tree or SDK installation. "
-        "Use URHO3D_HOME environment variable or build option to specify the location of the build tree or SDK installation. ${NOT_FOUND_MESSAGE}")
+        "Could NOT find Urho3D library in Urho3D SDK installation or build tree. "
+        "Use URHO3D_HOME environment variable or build option to specify the location of the non-default SDK installation or build tree. ${NOT_FOUND_MESSAGE}")
 endif ()
 
 mark_as_advanced (URHO3D_BASE_INCLUDE_DIR URHO3D_LIBRARIES URHO3D_LIBRARIES_DBG URHO3D_DLL_REL URHO3D_DLL_DBG URHO3D_HOME)

+ 1 - 1
Docs/GettingStarted.dox

@@ -265,7 +265,7 @@ The build process first builds the Urho3D library target (either static or share
 
 To install the Urho3D library as an SDK, use the usual 'make install' command. There is an equivalent built-in target called "install" in Visual Studio solution and Xcode project to perform the SDK installation. This could be useful when you want your application to always link against a 'stable' installed version of the Urho3D library, while keeping your Urho3D project root tree in sync with origin/master; or when you want other users in the same host system to use the installed Urho3D SDK instead of them building from source again.
 
-The default install prefix is 'c:/Program Files/Urho3D' on Windows host and '/usr/local' on all other non-Windows hosts. You can use the CMAKE_INSTALL_PREFIX build option to alter this prefix path. However, when cross-compiling you may actually want to alter the final installation destination by supplying DESDIR environment variable instead of altering the CMAKE_INSTALL_PREFIX variable directly, especially when using Urho3D library as shared library type because the installed executables have their RPATH adjusted relative to CMAKE_INSTALL_PREFIX. The DESTDIR environment variable is not applicable for installation on Windows host, i.e. it can only use CMAKE_INSTALL_PREFIX variable to alter the final installation destination. This is not a lost, however, because there is no RPATH adjustment for Windows platform, but as the result the Urho3D.dll will be installed in a same directory as the installed executables.
+The default install prefix is 'c:/Program Files/Urho3D' on Windows host and '/usr/local' on all other non-Windows hosts. You can use the CMAKE_INSTALL_PREFIX build option to alter this prefix path. However, when cross-compiling you may actually want to alter the final installation destination by supplying DESTDIR environment variable instead of altering the CMAKE_INSTALL_PREFIX variable directly, especially when using Urho3D library as shared library type because the installed executables have their RPATH adjusted relative to CMAKE_INSTALL_PREFIX. The DESTDIR environment variable is not applicable for installation on Windows host, i.e. it can only use CMAKE_INSTALL_PREFIX variable to alter the final installation destination. This is not a lost, however, because there is no RPATH adjustment for Windows platform, but as the result the Urho3D.dll will be installed in a same directory as the installed executables.
 
 If the Urho3D SDK is installed to a system-wide default location then the Urho3D library can be found by FindUrho3D CMake module automatically without the help of URHO3D_HOME environment variable or build option. When cross-compiling, the system-wide default location is usually in the system root of the cross-compiling target. To get a corresponding system root path for a cross-compiling build tree, you can use SYSROOT internal variable stored in the CMake cache. You can then use the system root path to set the DESTDIR environment variable in order to stage the Urho3D SDK installation in the corresponding system root. For example:
 

+ 4 - 1
Source/Urho3D/CMakeLists.txt

@@ -169,7 +169,10 @@ endif ()
 
 # Generate platform specific export header file
 if (MSVC)
-    set (PRE_EXPORT_HEADER "\n#pragma warning(disable: 4251)\n#pragma warning(disable: 4275)\n")
+    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}")
 endif ()
 if (URHO3D_CLANG_TOOLS)
     set (POST_EXPORT_HEADER "\n#define NONSCRIPTABLE __attribute__((annotate(\"nonscriptable\")))\n")