Explorar o código

Fix PCH generation and visibility warning on STATIC build.
Close #2652

Yao Wei Tjong 姚伟忠 %!s(int64=5) %!d(string=hai) anos
pai
achega
92cf44551f

+ 0 - 374
CMake/Modules/GenerateExportHeader.cmake

@@ -1,374 +0,0 @@
-# Distributed under the OSI-approved BSD 3-Clause License.  See accompanying
-# file Copyright.txt or https://cmake.org/licensing for details.
-
-#.rst:
-# GenerateExportHeader
-# --------------------
-#
-# Function for generation of export macros for libraries
-#
-# This module provides the function GENERATE_EXPORT_HEADER().
-#
-# The ``GENERATE_EXPORT_HEADER`` function can be used to generate a file
-# suitable for preprocessor inclusion which contains EXPORT macros to be
-# used in library classes::
-#
-#    GENERATE_EXPORT_HEADER( LIBRARY_TARGET
-#              [BASE_NAME <base_name>]
-#              [EXPORT_MACRO_NAME <export_macro_name>]
-#              [EXPORT_FILE_NAME <export_file_name>]
-#              [DEPRECATED_MACRO_NAME <deprecated_macro_name>]
-#              [NO_EXPORT_MACRO_NAME <no_export_macro_name>]
-#              [STATIC_DEFINE <static_define>]
-#              [NO_DEPRECATED_MACRO_NAME <no_deprecated_macro_name>]
-#              [DEFINE_NO_DEPRECATED]
-#              [PREFIX_NAME <prefix_name>]
-#              [CUSTOM_CONTENT_FROM_VARIABLE <variable>]
-#    )
-#
-#
-# The target properties :prop_tgt:`CXX_VISIBILITY_PRESET <<LANG>_VISIBILITY_PRESET>`
-# and :prop_tgt:`VISIBILITY_INLINES_HIDDEN` can be used to add the appropriate
-# compile flags for targets.  See the documentation of those target properties,
-# and the convenience variables
-# :variable:`CMAKE_CXX_VISIBILITY_PRESET <CMAKE_<LANG>_VISIBILITY_PRESET>` and
-# :variable:`CMAKE_VISIBILITY_INLINES_HIDDEN`.
-#
-# By default ``GENERATE_EXPORT_HEADER()`` generates macro names in a file
-# name determined by the name of the library.  This means that in the
-# simplest case, users of ``GenerateExportHeader`` will be equivalent to:
-#
-# .. code-block:: cmake
-#
-#    set(CMAKE_CXX_VISIBILITY_PRESET hidden)
-#    set(CMAKE_VISIBILITY_INLINES_HIDDEN 1)
-#    add_library(somelib someclass.cpp)
-#    generate_export_header(somelib)
-#    install(TARGETS somelib DESTINATION ${LIBRARY_INSTALL_DIR})
-#    install(FILES
-#     someclass.h
-#     ${PROJECT_BINARY_DIR}/somelib_export.h DESTINATION ${INCLUDE_INSTALL_DIR}
-#    )
-#
-#
-# And in the ABI header files:
-#
-# .. code-block:: c++
-#
-#    #include "somelib_export.h"
-#    class SOMELIB_EXPORT SomeClass {
-#      ...
-#    };
-#
-#
-# The CMake fragment will generate a file in the
-# ``${CMAKE_CURRENT_BINARY_DIR}`` called ``somelib_export.h`` containing the
-# macros ``SOMELIB_EXPORT``, ``SOMELIB_NO_EXPORT``, ``SOMELIB_DEPRECATED``,
-# ``SOMELIB_DEPRECATED_EXPORT`` and ``SOMELIB_DEPRECATED_NO_EXPORT``.
-# They will be followed by content taken from the variable specified by
-# the ``CUSTOM_CONTENT_FROM_VARIABLE`` option, if any.
-# The resulting file should be installed with other headers in the library.
-#
-# The ``BASE_NAME`` argument can be used to override the file name and the
-# names used for the macros:
-#
-# .. code-block:: cmake
-#
-#    add_library(somelib someclass.cpp)
-#    generate_export_header(somelib
-#      BASE_NAME other_name
-#    )
-#
-#
-# Generates a file called ``other_name_export.h`` containing the macros
-# ``OTHER_NAME_EXPORT``, ``OTHER_NAME_NO_EXPORT`` and ``OTHER_NAME_DEPRECATED``
-# etc.
-#
-# The ``BASE_NAME`` may be overridden by specifying other options in the
-# function.  For example:
-#
-# .. code-block:: cmake
-#
-#    add_library(somelib someclass.cpp)
-#    generate_export_header(somelib
-#      EXPORT_MACRO_NAME OTHER_NAME_EXPORT
-#    )
-#
-#
-# creates the macro ``OTHER_NAME_EXPORT`` instead of ``SOMELIB_EXPORT``, but
-# other macros and the generated file name is as default:
-#
-# .. code-block:: cmake
-#
-#    add_library(somelib someclass.cpp)
-#    generate_export_header(somelib
-#      DEPRECATED_MACRO_NAME KDE_DEPRECATED
-#    )
-#
-#
-# creates the macro ``KDE_DEPRECATED`` instead of ``SOMELIB_DEPRECATED``.
-#
-# If ``LIBRARY_TARGET`` is a static library, macros are defined without
-# values.
-#
-# If the same sources are used to create both a shared and a static
-# library, the uppercased symbol ``${BASE_NAME}_STATIC_DEFINE`` should be
-# used when building the static library:
-#
-# .. code-block:: cmake
-#
-#    add_library(shared_variant SHARED ${lib_SRCS})
-#    add_library(static_variant ${lib_SRCS})
-#    generate_export_header(shared_variant BASE_NAME libshared_and_static)
-#    set_target_properties(static_variant PROPERTIES
-#      COMPILE_FLAGS -DLIBSHARED_AND_STATIC_STATIC_DEFINE)
-#
-# This will cause the export macros to expand to nothing when building
-# the static library.
-#
-# If ``DEFINE_NO_DEPRECATED`` is specified, then a macro
-# ``${BASE_NAME}_NO_DEPRECATED`` will be defined This macro can be used to
-# remove deprecated code from preprocessor output:
-#
-# .. code-block:: cmake
-#
-#    option(EXCLUDE_DEPRECATED "Exclude deprecated parts of the library" FALSE)
-#    if (EXCLUDE_DEPRECATED)
-#      set(NO_BUILD_DEPRECATED DEFINE_NO_DEPRECATED)
-#    endif()
-#    generate_export_header(somelib ${NO_BUILD_DEPRECATED})
-#
-#
-# And then in somelib:
-#
-# .. code-block:: c++
-#
-#    class SOMELIB_EXPORT SomeClass
-#    {
-#    public:
-#    #ifndef SOMELIB_NO_DEPRECATED
-#      SOMELIB_DEPRECATED void oldMethod();
-#    #endif
-#    };
-#
-# .. code-block:: c++
-#
-#    #ifndef SOMELIB_NO_DEPRECATED
-#    void SomeClass::oldMethod() {  }
-#    #endif
-#
-#
-# If ``PREFIX_NAME`` is specified, the argument will be used as a prefix to
-# all generated macros.
-#
-# For example:
-#
-# .. code-block:: cmake
-#
-#    generate_export_header(somelib PREFIX_NAME VTK_)
-#
-# Generates the macros ``VTK_SOMELIB_EXPORT`` etc.
-#
-# ::
-#
-#    ADD_COMPILER_EXPORT_FLAGS( [<output_variable>] )
-#
-# The ``ADD_COMPILER_EXPORT_FLAGS`` function adds ``-fvisibility=hidden`` to
-# :variable:`CMAKE_CXX_FLAGS <CMAKE_<LANG>_FLAGS>` if supported, and is a no-op
-# on Windows which does not need extra compiler flags for exporting support.
-# You may optionally pass a single argument to ``ADD_COMPILER_EXPORT_FLAGS``
-# that will be populated with the ``CXX_FLAGS`` required to enable visibility
-# support for the compiler/architecture in use.
-#
-# This function is deprecated.  Set the target properties
-# :prop_tgt:`CXX_VISIBILITY_PRESET <<LANG>_VISIBILITY_PRESET>` and
-# :prop_tgt:`VISIBILITY_INLINES_HIDDEN` instead.
-
-# Modified by Yao Wei Tjong for Urho3D
-
-include(CheckCXXCompilerFlag)
-
-# TODO: Install this macro separately?
-macro(_check_cxx_compiler_attribute _ATTRIBUTE _RESULT)
-  check_cxx_source_compiles("${_ATTRIBUTE} int somefunc() { return 0; }
-    int main() { return somefunc();}" ${_RESULT}
-  )
-endmacro()
-
-macro(_test_compiler_hidden_visibility)
-
-  if(CMAKE_COMPILER_IS_GNUCXX AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "4.2")
-    set(GCC_TOO_OLD TRUE)
-  elseif(CMAKE_COMPILER_IS_GNUCC AND CMAKE_C_COMPILER_VERSION VERSION_LESS "4.2")
-    set(GCC_TOO_OLD TRUE)
-  elseif(CMAKE_CXX_COMPILER_ID MATCHES Intel AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS "12.0")
-    set(_INTEL_TOO_OLD TRUE)
-  endif()
-
-  # Exclude XL here because it misinterprets -fvisibility=hidden even though
-  # the check_cxx_compiler_flag passes
-  if(NOT GCC_TOO_OLD
-      AND NOT _INTEL_TOO_OLD
-      AND NOT WIN32
-      AND NOT CYGWIN
-      AND NOT CMAKE_CXX_COMPILER_ID MATCHES XL
-      AND NOT CMAKE_CXX_COMPILER_ID MATCHES PGI
-      AND NOT CMAKE_CXX_COMPILER_ID MATCHES Watcom)
-    # Urho3D - set the flags in a variable before testing
-    set (COMPILER_HIDDEN_VISIBILITY_FLAGS -fvisibility=hidden)
-    set (COMPILER_HIDDEN_INLINE_VISIBILITY_FLAGS -fvisibility-inlines-hidden)
-    check_cxx_compiler_flag(${COMPILER_HIDDEN_VISIBILITY_FLAGS} COMPILER_HAS_HIDDEN_VISIBILITY)
-    check_cxx_compiler_flag(${COMPILER_HIDDEN_INLINE_VISIBILITY_FLAGS} COMPILER_HAS_HIDDEN_INLINE_VISIBILITY)
-  endif()
-endmacro()
-
-macro(_test_compiler_has_deprecated)
-  # NOTE:  Some Embarcadero compilers silently compile __declspec(deprecated)
-  # without error, but this is not a documented feature and the attribute does
-  # not actually generate any warnings.
-  if(CMAKE_CXX_COMPILER_ID MATCHES Borland
-      OR CMAKE_CXX_COMPILER_ID MATCHES Embarcadero
-      OR CMAKE_CXX_COMPILER_ID MATCHES HP
-      OR GCC_TOO_OLD
-      OR CMAKE_CXX_COMPILER_ID MATCHES PGI
-      OR CMAKE_CXX_COMPILER_ID MATCHES Watcom)
-    set(COMPILER_HAS_DEPRECATED "" CACHE INTERNAL
-      "Compiler support for a deprecated attribute")
-  else()
-    _check_cxx_compiler_attribute("__attribute__((__deprecated__))"
-      COMPILER_HAS_DEPRECATED_ATTR)
-    if(COMPILER_HAS_DEPRECATED_ATTR)
-      set(COMPILER_HAS_DEPRECATED "${COMPILER_HAS_DEPRECATED_ATTR}"
-        CACHE INTERNAL "Compiler support for a deprecated attribute")
-    else()
-      _check_cxx_compiler_attribute("__declspec(deprecated)"
-        COMPILER_HAS_DEPRECATED)
-    endif()
-  endif()
-endmacro()
-
-get_filename_component(_GENERATE_EXPORT_HEADER_MODULE_DIR
-  "${CMAKE_CURRENT_LIST_FILE}" PATH)
-
-macro(_DO_SET_MACRO_VALUES TARGET_LIBRARY)
-  set(DEFINE_DEPRECATED)
-  set(DEFINE_EXPORT)
-  set(DEFINE_IMPORT)
-  set(DEFINE_NO_EXPORT)
-
-  if (COMPILER_HAS_DEPRECATED_ATTR)
-    set(DEFINE_DEPRECATED "__attribute__ ((__deprecated__))")
-  elseif(COMPILER_HAS_DEPRECATED)
-    set(DEFINE_DEPRECATED "__declspec(deprecated)")
-  endif()
-
-  # Urho3D: always generate header file regardless of target type
-    if(WIN32 OR CYGWIN)
-      set(DEFINE_EXPORT "__declspec(dllexport)")
-      set(DEFINE_IMPORT "__declspec(dllimport)")
-    elseif(COMPILER_HAS_HIDDEN_VISIBILITY)
-      set(DEFINE_EXPORT "__attribute__((visibility(\"default\")))")
-      set(DEFINE_IMPORT "__attribute__((visibility(\"default\")))")
-      set(DEFINE_NO_EXPORT "__attribute__((visibility(\"hidden\")))")
-    endif()
-endmacro()
-
-macro(_DO_GENERATE_EXPORT_HEADER TARGET_LIBRARY)
-  # Option overrides
-  set(options DEFINE_NO_DEPRECATED)
-  set(oneValueArgs PREFIX_NAME BASE_NAME EXPORT_MACRO_NAME EXPORT_FILE_NAME
-    DEPRECATED_MACRO_NAME NO_EXPORT_MACRO_NAME STATIC_DEFINE
-    NO_DEPRECATED_MACRO_NAME CUSTOM_CONTENT_FROM_VARIABLE)
-  set(multiValueArgs)
-
-  cmake_parse_arguments(_GEH "${options}" "${oneValueArgs}" "${multiValueArgs}"
-    ${ARGN})
-
-  set(BASE_NAME "${TARGET_LIBRARY}")
-
-  if(_GEH_BASE_NAME)
-    set(BASE_NAME ${_GEH_BASE_NAME})
-  endif()
-
-  string(TOUPPER ${BASE_NAME} BASE_NAME_UPPER)
-  string(TOLOWER ${BASE_NAME} BASE_NAME_LOWER)
-
-  # Default options
-  set(EXPORT_MACRO_NAME "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_EXPORT")
-  set(NO_EXPORT_MACRO_NAME "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_NO_EXPORT")
-  set(EXPORT_FILE_NAME "${CMAKE_CURRENT_BINARY_DIR}/${BASE_NAME_LOWER}_export.h")
-  set(DEPRECATED_MACRO_NAME "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_DEPRECATED")
-  set(STATIC_DEFINE "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_STATIC_DEFINE")
-  set(NO_DEPRECATED_MACRO_NAME
-    "${_GEH_PREFIX_NAME}${BASE_NAME_UPPER}_NO_DEPRECATED")
-
-  if(_GEH_UNPARSED_ARGUMENTS)
-    message(FATAL_ERROR "Unknown keywords given to GENERATE_EXPORT_HEADER(): \"${_GEH_UNPARSED_ARGUMENTS}\"")
-  endif()
-
-  if(_GEH_EXPORT_MACRO_NAME)
-    set(EXPORT_MACRO_NAME ${_GEH_PREFIX_NAME}${_GEH_EXPORT_MACRO_NAME})
-  endif()
-  string(MAKE_C_IDENTIFIER ${EXPORT_MACRO_NAME} EXPORT_MACRO_NAME)
-  if(_GEH_EXPORT_FILE_NAME)
-    if(IS_ABSOLUTE ${_GEH_EXPORT_FILE_NAME})
-      set(EXPORT_FILE_NAME ${_GEH_EXPORT_FILE_NAME})
-    else()
-      set(EXPORT_FILE_NAME "${CMAKE_CURRENT_BINARY_DIR}/${_GEH_EXPORT_FILE_NAME}")
-    endif()
-  endif()
-  if(_GEH_DEPRECATED_MACRO_NAME)
-    set(DEPRECATED_MACRO_NAME ${_GEH_PREFIX_NAME}${_GEH_DEPRECATED_MACRO_NAME})
-  endif()
-  string(MAKE_C_IDENTIFIER ${DEPRECATED_MACRO_NAME} DEPRECATED_MACRO_NAME)
-  if(_GEH_NO_EXPORT_MACRO_NAME)
-    set(NO_EXPORT_MACRO_NAME ${_GEH_PREFIX_NAME}${_GEH_NO_EXPORT_MACRO_NAME})
-  endif()
-  string(MAKE_C_IDENTIFIER ${NO_EXPORT_MACRO_NAME} NO_EXPORT_MACRO_NAME)
-  if(_GEH_STATIC_DEFINE)
-    set(STATIC_DEFINE ${_GEH_PREFIX_NAME}${_GEH_STATIC_DEFINE})
-  endif()
-  string(MAKE_C_IDENTIFIER ${STATIC_DEFINE} STATIC_DEFINE)
-
-  if(_GEH_DEFINE_NO_DEPRECATED)
-    set(DEFINE_NO_DEPRECATED 1)
-  else()
-    set(DEFINE_NO_DEPRECATED 0)
-  endif()
-
-  if(_GEH_NO_DEPRECATED_MACRO_NAME)
-    set(NO_DEPRECATED_MACRO_NAME
-      ${_GEH_PREFIX_NAME}${_GEH_NO_DEPRECATED_MACRO_NAME})
-  endif()
-  string(MAKE_C_IDENTIFIER ${NO_DEPRECATED_MACRO_NAME} NO_DEPRECATED_MACRO_NAME)
-
-  set(INCLUDE_GUARD_NAME "${EXPORT_MACRO_NAME}_H")
-
-  # 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)
-
-  if(_GEH_CUSTOM_CONTENT_FROM_VARIABLE)
-    if(DEFINED "${_GEH_CUSTOM_CONTENT_FROM_VARIABLE}")
-      set(CUSTOM_CONTENT "${${_GEH_CUSTOM_CONTENT_FROM_VARIABLE}}")
-    else()
-      set(CUSTOM_CONTENT "")
-    endif()
-  endif()
-
-  configure_file("${_GENERATE_EXPORT_HEADER_MODULE_DIR}/exportheader.cmake.in"
-    "${EXPORT_FILE_NAME}" @ONLY)
-endmacro()
-
-# Urho3D: revise to pass the library type by argument so that it does not depend on the target to be added first
-function(GENERATE_EXPORT_HEADER TARGET_LIBRARY type)
-  if(NOT ${type} MATCHES STATIC|SHARED|OBJECT|MODULE)
-    message(WARNING "This macro can only be used with libraries")
-    return()
-  endif()
-  _test_compiler_hidden_visibility()
-  _test_compiler_has_deprecated()
-  _do_set_macro_values(${TARGET_LIBRARY} ${type})
-  _do_generate_export_header(${TARGET_LIBRARY} ${ARGN})
-endfunction()
-

+ 0 - 35
CMake/Modules/PostProcessForWebModule.cmake

@@ -1,35 +0,0 @@
-#
-# Copyright (c) 2008-2020 the Urho3D project.
-#
-# Permission is hereby granted, free of charge, to any person obtaining a copy
-# of this software and associated documentation files (the "Software"), to deal
-# in the Software without restriction, including without limitation the rights
-# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-# copies of the Software, and to permit persons to whom the Software is
-# furnished to do so, subject to the following conditions:
-#
-# The above copyright notice and this permission notice shall be included in
-# all copies or substantial portions of the Software.
-#
-# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-# THE SOFTWARE.
-#
-
-# Post process to glue the main module and side module(s) together
-
-string (REPLACE " " .js',' SIDE_MODULES "'${SIDE_MODULES}.js'")              # Stringify for string replacement
-if (HAS_SHELL_FILE)
-    file (READ ${TARGET_FILE} CONTENT)
-    string (REPLACE ${TARGET_NAME}.js libUrho3D.js CONTENT "${CONTENT}")     # Stringify to preserve semicolons
-    # Assume HTML shell-file has Module object without the 'dynamicLibraries' prop defined yet
-    string (REGEX REPLACE "(var Module *= *{)" \\1dynamicLibraries:[${SIDE_MODULES}], CONTENT "${CONTENT}")
-    file (WRITE ${TARGET_FILE} "${CONTENT}")
-else ()
-    file (READ ${TARGET_DIR}/libUrho3D.js CONTENT)
-    file (WRITE ${TARGET_DIR}/${TARGET_NAME}.main.js "var Module={dynamicLibraries:[${SIDE_MODULES}]};${CONTENT}")
-endif ()

+ 36 - 42
CMake/Modules/UrhoCommon.cmake

@@ -1140,7 +1140,6 @@ endmacro ()
 include (GenerateExportHeader)
 
 # Macro for precompiling header (On MSVC, the dummy C++ or C implementation file for precompiling the header file would be generated if not already exists)
-# This macro should be called before the CMake target has been added
 # Typically, user should indirectly call this macro by using the 'PCH' option when calling define_source_files() macro
 macro (enable_pch HEADER_PATHNAME)
     # No op when PCH support is not enabled
@@ -1171,10 +1170,24 @@ macro (enable_pch HEADER_PATHNAME)
             # Clang or MSVC
             set (PCH_FILENAME ${HEADER_FILENAME}.pch)
         endif ()
-
-        if (MSVC)
-            get_filename_component (NAME_WE ${HEADER_FILENAME} NAME_WE)
-            if (TARGET ${TARGET_NAME})
+        if (TARGET ${TARGET_NAME})
+            if (MSVC)
+                # Add the dummy C++ or C implementation file if necessary
+                get_filename_component (NAME_WE ${HEADER_FILENAME} NAME_WE)
+                set (${LANG}_FILENAME ${NAME_WE}.${EXT})
+                get_filename_component (PATH ${HEADER_PATHNAME} PATH)
+                if (PATH)
+                    set (PATH ${PATH}/)
+                endif ()
+                list (FIND SOURCE_FILES ${PATH}${${LANG}_FILENAME} ${LANG}_FILENAME_FOUND)
+                if (${LANG}_FILENAME_FOUND STREQUAL -1)
+                    if (NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/${${LANG}_FILENAME})
+                        # Only generate it once so that its timestamp is not touched unnecessarily
+                        file (WRITE ${CMAKE_CURRENT_BINARY_DIR}/${${LANG}_FILENAME} "// This is a generated file. DO NOT EDIT!\n\n#include \"${HEADER_FILENAME}\"")
+                    endif ()
+                    target_sources (${TARGET_NAME} PRIVATE ${${LANG}_FILENAME})
+                endif ()
+                source_group ("Source Files\\Generated" FILES ${${LANG}_FILENAME})
                 if (VS)
                     # VS is multi-config, the exact path is only known during actual build time based on effective build config
                     set (PCH_PATHNAME "$(IntDir)${PCH_FILENAME}")
@@ -1193,46 +1206,17 @@ macro (enable_pch HEADER_PATHNAME)
                     endif ()
                 endforeach ()
                 unset (${TARGET_NAME}_HEADER_PATHNAME)
-            else ()
-                # The target has not been created yet, so set an internal variable to come back here again later
-                set (${TARGET_NAME}_HEADER_PATHNAME ${ARGV})
-                # But proceed to add the dummy C++ or C implementation file if necessary
-                set (${LANG}_FILENAME ${NAME_WE}.${EXT})
-                get_filename_component (PATH ${HEADER_PATHNAME} PATH)
-                if (PATH)
-                    set (PATH ${PATH}/)
-                endif ()
-                list (FIND SOURCE_FILES ${PATH}${${LANG}_FILENAME} ${LANG}_FILENAME_FOUND)
-                if (${LANG}_FILENAME_FOUND STREQUAL -1)
-                    if (NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/${${LANG}_FILENAME})
-                        # Only generate it once so that its timestamp is not touched unnecessarily
-                        file (WRITE ${CMAKE_CURRENT_BINARY_DIR}/${${LANG}_FILENAME} "// This is a generated file. DO NOT EDIT!\n\n#include \"${HEADER_FILENAME}\"")
-                    endif ()
-                    list (INSERT SOURCE_FILES 0 ${${LANG}_FILENAME})
-                endif ()
-                source_group ("Source Files\\Generated" FILES ${${LANG}_FILENAME})
-            endif ()
-        elseif (XCODE)
-            if (TARGET ${TARGET_NAME})
+            elseif (XCODE)
                 # Precompiling and using precompiled header file
                 set_target_properties (${TARGET_NAME} PROPERTIES XCODE_ATTRIBUTE_GCC_PRECOMPILE_PREFIX_HEADER YES XCODE_ATTRIBUTE_GCC_PREFIX_HEADER ${ABS_HEADER_PATHNAME})
                 unset (${TARGET_NAME}_HEADER_PATHNAME)
-            else ()
-                # The target has not been created yet, so set an internal variable to come back here again later
-                set (${TARGET_NAME}_HEADER_PATHNAME ${ARGV})
-            endif ()
-        else ()
-            # GCC or Clang
-            if (TARGET ${TARGET_NAME})
+            else () # GCC or Clang
                 # Precompiling header file
                 get_directory_property (COMPILE_DEFINITIONS COMPILE_DEFINITIONS)
                 get_directory_property (INCLUDE_DIRECTORIES INCLUDE_DIRECTORIES)
                 get_target_property (TYPE ${TARGET_NAME} TYPE)
                 if (TYPE MATCHES SHARED)
                     list (APPEND COMPILE_DEFINITIONS ${TARGET_NAME}_EXPORTS)
-                    if (LANG STREQUAL CXX)
-                        _test_compiler_hidden_visibility ()
-                    endif ()
                 endif ()
                 # Use PIC flags as necessary, except when compiling using MinGW which already uses PIC flags for all codes
                 if (NOT MINGW)
@@ -1241,6 +1225,16 @@ macro (enable_pch HEADER_PATHNAME)
                         set (PIC_FLAGS -fPIC)
                     endif ()
                 endif ()
+                get_target_property (VISIBILITY_PRESET ${TARGET_NAME} ${LANG}_VISIBILITY_PRESET)
+                set (CVP -fvisibility=${VISIBILITY_PRESET})
+                get_target_property (VISIBILITY_INLINES_HIDDEN ${TARGET_NAME} VISIBILITY_INLINES_HIDDEN)
+                if (VISIBILITY_INLINES_HIDDEN)
+                    set (VID -fvisibility-inlines-hidden)
+                endif ()
+                if (LANG STREQUAL CXX)
+                    get_target_property (CXX_STANDARD ${TARGET_NAME} CXX_STANDARD)
+                    set (CXX_STANDARD -std=c++${CXX_STANDARD})
+                endif ()
                 string (REPLACE ";" " -D" COMPILE_DEFINITIONS "-D${COMPILE_DEFINITIONS}")
                 string (REPLACE "\"" "\\\"" COMPILE_DEFINITIONS ${COMPILE_DEFINITIONS})
                 string (REPLACE ";" "\" -I\"" INCLUDE_DIRECTORIES "-I\"${INCLUDE_DIRECTORIES}\"")
@@ -1253,7 +1247,7 @@ macro (enable_pch HEADER_PATHNAME)
                 foreach (CONFIG ${CMAKE_CONFIGURATION_TYPES} ${CMAKE_BUILD_TYPE})   # These two vars are mutually exclusive
                     # Generate *.rsp containing configuration specific compiler flags
                     string (TOUPPER ${CONFIG} UPPERCASE_CONFIG)
-                    file (WRITE ${ABS_PATH_PCH}.${CONFIG}.pch.rsp.new "${COMPILE_DEFINITIONS} ${SYSROOT_FLAGS} ${CLANG_${LANG}_FLAGS} ${CMAKE_${LANG}_FLAGS} ${CMAKE_${LANG}_FLAGS_${UPPERCASE_CONFIG}} ${COMPILER_HIDDEN_VISIBILITY_FLAGS} ${COMPILER_HIDDEN_INLINE_VISIBILITY_FLAGS} ${PIC_FLAGS} -std=c++11 ${INCLUDE_DIRECTORIES} -c -x ${LANG_H}")
+                    file (WRITE ${ABS_PATH_PCH}.${CONFIG}.pch.rsp.new "${COMPILE_DEFINITIONS} ${SYSROOT_FLAGS} ${CLANG_${LANG}_FLAGS} ${CMAKE_${LANG}_FLAGS} ${CMAKE_${LANG}_FLAGS_${UPPERCASE_CONFIG}} ${PIC_FLAGS} ${CVP} ${VID} ${CXX_STANDARD} ${INCLUDE_DIRECTORIES} -c -x ${LANG_H}")
                     execute_process (COMMAND ${CMAKE_COMMAND} -E copy_if_different ${ABS_PATH_PCH}.${CONFIG}.pch.rsp.new ${ABS_PATH_PCH}.${CONFIG}.pch.rsp)
                     file (REMOVE ${ABS_PATH_PCH}.${CONFIG}.pch.rsp.new)
                     if (NOT ${TARGET_NAME}_PCH_DEPS)
@@ -1282,10 +1276,7 @@ macro (enable_pch HEADER_PATHNAME)
                 # Using precompiled header file
                 set (CMAKE_${LANG}_FLAGS "${CMAKE_${LANG}_FLAGS} -include \"${ABS_PATH_PCH}\" -Winvalid-pch")   # Catch the invalid PCH sooner
                 unset (${TARGET_NAME}_HEADER_PATHNAME)
-            else ()
-                # The target has not been created yet, so set an internal variable to come back here again later
-                set (${TARGET_NAME}_HEADER_PATHNAME ${ARGV})
-                # But proceed to add the dummy source file(s) to trigger the custom command output rule
+                # Add the dummy source file(s) to trigger the custom command output rule
                 if (CMAKE_CONFIGURATION_TYPES)
                     # Multi-config, trigger all rules and let the compiler to choose which precompiled header is suitable to use
                     foreach (CONFIG ${CMAKE_CONFIGURATION_TYPES})
@@ -1295,8 +1286,11 @@ macro (enable_pch HEADER_PATHNAME)
                     # Single-config, just trigger the corresponding rule matching the current build configuration
                     set (TRIGGERS ${HEADER_FILENAME}.${CMAKE_BUILD_TYPE}.pch.trigger)
                 endif ()
-                list (APPEND SOURCE_FILES ${TRIGGERS})
+                target_sources (${TARGET_NAME} PRIVATE ${TRIGGERS})
             endif ()
+        else ()
+            # The target has not been created yet, so set an internal variable to come back here again later
+            set (${TARGET_NAME}_HEADER_PATHNAME ${ARGV})
         endif ()
     endif ()
 endmacro ()

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

@@ -1,76 +0,0 @@
-//
-// Copyright (c) 2008-2020 the Urho3D project.
-//
-// Permission is hereby granted, free of charge, to any person obtaining a copy
-// of this software and associated documentation files (the "Software"), to deal
-// in the Software without restriction, including without limitation the rights
-// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-// copies of the Software, and to permit persons to whom the Software is
-// furnished to do so, subject to the following conditions:
-//
-// The above copyright notice and this permission notice shall be included in
-// all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-// THE SOFTWARE.
-//
-
-#ifndef @INCLUDE_GUARD_NAME@
-#define @INCLUDE_GUARD_NAME@
-@PRE_EXPORT_HEADER@
-#cmakedefine URHO3D_STATIC_DEFINE
-#cmakedefine URHO3D_OPENGL
-#cmakedefine URHO3D_D3D11
-#cmakedefine URHO3D_SSE
-#cmakedefine URHO3D_DATABASE_ODBC
-#cmakedefine URHO3D_DATABASE_SQLITE
-#cmakedefine URHO3D_LUAJIT
-#cmakedefine URHO3D_TESTING
-
-#cmakedefine CLANG_PRE_STANDARD
-
-#ifdef @STATIC_DEFINE@
-#  define @EXPORT_MACRO_NAME@
-#  define @NO_EXPORT_MACRO_NAME@
-#else
-#  ifndef @EXPORT_MACRO_NAME@
-#    ifdef @EXPORT_IMPORT_CONDITION@
-        /* We are building this library */
-#      define @EXPORT_MACRO_NAME@ @DEFINE_EXPORT@
-#    else
-        /* We are using this library */
-#      define @EXPORT_MACRO_NAME@ @DEFINE_IMPORT@
-#    endif
-#  endif
-
-#  ifndef @NO_EXPORT_MACRO_NAME@
-#    define @NO_EXPORT_MACRO_NAME@ @DEFINE_NO_EXPORT@
-#  endif
-#endif
-
-#ifndef @DEPRECATED_MACRO_NAME@
-#  define @DEPRECATED_MACRO_NAME@ @DEFINE_DEPRECATED@
-#endif
-
-#ifndef @DEPRECATED_MACRO_NAME@_EXPORT
-#  define @DEPRECATED_MACRO_NAME@_EXPORT @EXPORT_MACRO_NAME@ @DEPRECATED_MACRO_NAME@
-#endif
-
-#ifndef @DEPRECATED_MACRO_NAME@_NO_EXPORT
-#  define @DEPRECATED_MACRO_NAME@_NO_EXPORT @NO_EXPORT_MACRO_NAME@ @DEPRECATED_MACRO_NAME@
-#endif
-
-#if @DEFINE_NO_DEPRECATED@ /* DEFINE_NO_DEPRECATED */
-#  ifndef @NO_DEPRECATED_MACRO_NAME@
-#    define @NO_DEPRECATED_MACRO_NAME@
-#  endif
-#endif
-@CUSTOM_CONTENT@
-#define NONSCRIPTABLE @ANNOTATE_NONSCRIPTABLE@
-@POST_EXPORT_HEADER@
-#endif

+ 25 - 14
Source/Urho3D/CMakeLists.txt

@@ -154,7 +154,7 @@ if (APPLE AND NOT ARM)
     set (GLOB_OBJC_PATTERN *.m)     # Should only pick up MacFileWatcher.m for macOS platform at the moment
 endif ()
 string (REPLACE ";" "/[^;]+;" EXCLUDE_PATTERNS "${EXCLUDED_SOURCE_DIRS};")
-define_source_files (EXCLUDE_PATTERNS ${EXCLUDE_PATTERNS} GLOB_CPP_PATTERNS *.cpp ${GLOB_OBJC_PATTERN} RECURSE GROUP PCH Precompiled.h)
+define_source_files (EXCLUDE_PATTERNS ${EXCLUDE_PATTERNS} GLOB_CPP_PATTERNS *.cpp ${GLOB_OBJC_PATTERN} RECURSE GROUP)
 
 # Define generated source files
 if (NOT EXISTS ${CMAKE_CURRENT_BINARY_DIR}/librevision.h)
@@ -260,18 +260,6 @@ if (URHO3D_LUA)
     source_group ("Source Files\\LuaScript\\Generated" FILES ${LUA_GEN_CPP_FILES})
 endif ()
 
-# Generate platform specific export header file
-if (MSVC)
-    set (PRE_EXPORT_HEADER "\n#pragma warning(disable: 4251)\n#pragma warning(disable: 4275)\n\n#if _MSC_VER < 1900\n#define strtoll _strtoi64\n#define strtoull _strtoui64\n#endif\n\n")
-endif ()
-if (URHO3D_CLANG_TOOLS)
-    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)
-file (REMOVE ${CMAKE_CURRENT_BINARY_DIR}/Urho3D.h.new)
-source_group ("Source Files\\Generated" FILES ${CMAKE_CURRENT_BINARY_DIR}/Urho3D.h)
-
 # Aggregate all source files
 list (APPEND SOURCE_FILES ${GEN_CPP_FILES} ${LUA_GEN_CPP_FILES} librevision.h Urho3D.h ${SYMBOLIC_SOURCES})
 
@@ -308,9 +296,32 @@ endif ()
 if (IOS OR TVOS)
     set (EXCLUDE_FROM_ALL EXCLUDE_FROM_ALL)
 endif ()
-
 setup_library (${URHO3D_LIB_TYPE} ${EXCLUDE_FROM_ALL})
 
+# Generate platform specific export header file (can only be called after the target has been added)
+if (MSVC)
+    set (APPENDIX "\n#pragma warning(disable: 4251)\n#pragma warning(disable: 4275)\n\n#if _MSC_VER < 1900\n#define strtoll _strtoi64\n#define strtoull _strtoui64\n#endif\n")
+endif ()
+if (URHO3D_CLANG_TOOLS)
+    set (ANNOTATE_NONSCRIPTABLE "__attribute__((annotate(\"nonscriptable\")))")
+endif ()
+set (APPENDIX "${APPENDIX}\n#define NONSCRIPTABLE ${ANNOTATE_NONSCRIPTABLE}\n\n")
+foreach (DEFINE URHO3D_STATIC_DEFINE URHO3D_OPENGL URHO3D_D3D11 URHO3D_SSE URHO3D_DATABASE_ODBC URHO3D_DATABASE_SQLITE URHO3D_LUAJIT URHO3D_TESTING CLANG_PRE_STANDARD)
+    if (${DEFINE})
+        set (APPENDIX "${APPENDIX}#define ${DEFINE}\n")
+    endif ()
+endforeach()
+generate_export_header (${TARGET_NAME} EXPORT_MACRO_NAME URHO3D_API EXPORT_FILE_NAME Urho3D.h.new CUSTOM_CONTENT_FROM_VARIABLE APPENDIX)
+file (READ Precompiled.h COPYRIGHT LIMIT 1128)
+file (READ ${CMAKE_CURRENT_BINARY_DIR}/Urho3D.h.new EXPORT_HEADER)
+file (WRITE ${CMAKE_CURRENT_BINARY_DIR}/Urho3D.h.new ${COPYRIGHT}${EXPORT_HEADER})
+execute_process (COMMAND ${CMAKE_COMMAND} -E copy_if_different ${CMAKE_CURRENT_BINARY_DIR}/Urho3D.h.new ${CMAKE_CURRENT_BINARY_DIR}/Urho3D.h)
+file (REMOVE ${CMAKE_CURRENT_BINARY_DIR}/Urho3D.h.new)
+source_group ("Source Files\\Generated" FILES ${CMAKE_CURRENT_BINARY_DIR}/Urho3D.h)
+
+# Precompile the header after the export header file has been generated
+enable_pch (Precompiled.h)
+
 if (NOT ANDROID AND NOT WEB)
     file (READ .soversion SOVERSION)
     string (STRIP ${SOVERSION} SOVERSION)