瀏覽代碼

Merge pull request #4757 from assimp/kimkulling/Fix_link_openddl_static_when_enabling_static_lib_issue-4516

Use correct flags for openddl for static libs
Kim Kulling 2 年之前
父節點
當前提交
a3c209f2ca
共有 2 個文件被更改,包括 155 次插入113 次删除
  1. 1 0
      CMakeLists.txt
  2. 154 113
      contrib/openddlparser/CMakeLists.txt

+ 1 - 0
CMakeLists.txt

@@ -393,6 +393,7 @@ if(${BUILD_SHARED_LIBS})
   set(BUILD_LIB_TYPE SHARED)
 else()
   set(BUILD_LIB_TYPE STATIC)
+  add_definitions(-DDDL_STATIC_LIBRARY=OFF)
 endif()
 
 IF( UNIX )

+ 154 - 113
contrib/openddlparser/CMakeLists.txt

@@ -1,57 +1,50 @@
-CMAKE_MINIMUM_REQUIRED( VERSION 3.10 )
+CMAKE_MINIMUM_REQUIRED( VERSION 3.8 )
 PROJECT( OpenDDL-Parser )
-SET ( OPENDDL_PARSER_VERSION_MAJOR 0 )
-SET ( OPENDDL_PARSER_VERSION_MINOR 1 )
-SET ( OPENDDL_PARSER_VERSION_PATCH 0 )
-SET ( OPENDDL_PARSER_VERSION ${OPENDDL_PARSER_VERSION_MAJOR}.${OPENDDL_PARSER_VERSION_MINOR}.${OPENDDL_PARSER_VERSION_PATCH} )
-SET ( PROJECT_VERSION "${OPENDDL_PARSER_VERSION}" )
-
-option( DDL_DEBUG_OUTPUT        "Set to ON to use output debug texts"                                         OFF )
-option( DDL_STATIC_LIBRARY		"Set to ON to build static libary of OpenDDL Parser."                         ON )
-option( COVERALLS               "Generate coveralls data"                                                     OFF )
-
-set(CMAKE_CXX_STANDARD 11)
-set(CMAKE_CXX_STANDARD_REQUIRED ON)
-
-if( CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX )
-    find_package(Threads)
-else()
-    add_definitions( -D_CRT_SECURE_NO_WARNINGS )
+# read version number from cpp file of the form: static const char *Version = "0.4.0";
+file ( READ code/OpenDDLParser.cpp _ver )
+string( REGEX MATCH "static const char [*]Version[ ]*=[ ]*\"[^\"]*\"" _ver_line "${_ver}" )
+string( REGEX MATCH "[0-9]+\.[0-9]+\.[0-9]+" openddlparser_VERSION "${_ver_line}" )
+SET ( PROJECT_VERSION "${openddlparser_VERSION}" )
+if ( "${PROJECT_VERSION}" STREQUAL "" )
+    message( FATAL_ERROR "Cannot find 'static const char *Version' in 'code/OpenDDLParser.cpp'" )
 endif()
+message(STATUS "openddlparser_VERSION: ${openddlparser_VERSION}")
 
+option( DDL_DEBUG_OUTPUT        "Set to ON to use output debug texts"                                         OFF )
+option( DDL_STATIC_LIBRARY		"Deprecated, use BUILD_SHARED_LIBS instead."                                  ON )
+# for backwards compatibility use DDL_STATIC_LIBRARY as initial value for cmake variable BUILD_SHARED_LIBS
+# https://cmake.org/cmake/help/latest/variable/BUILD_SHARED_LIBS.html
 if ( DDL_STATIC_LIBRARY )
-	add_definitions( -DOPENDDL_STATIC_LIBARY )
+    set ( build_shared_libs_default OFF )
+else()
+    set ( build_shared_libs_default ON )
 endif()
+option( DDL_BUILD_SHARED_LIBS   "Set to ON to build shared libary of OpenDDL Parser."                         ${build_shared_libs_default} )
+option( COVERALLS               "Generate coveralls data"                                                     OFF )
+option( DDL_DOCUMENTATION       "Set to ON to opt in generating API documentation with Doxygen"               OFF )
+option( DDL_BUILD_TESTS         "Set to OFF to not build tests by default"                                    ON )
+option( DDL_BUILD_PARSER_DEMO   "Set to OFF to opt out building parser demo"                                  ON )
 
 if (MSVC)
-    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /D_SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING")
-    add_definitions(-DGTEST_HAS_TR1_TUPLE=0)
+    add_definitions(
+        -D_SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING
+        -DGTEST_HAS_TR1_TUPLE=0
+        -D_CRT_SECURE_NO_WARNINGS
+    )
 endif()
 
-add_definitions( -DOPENDDLPARSER_BUILD )
 add_definitions( -D_VARIADIC_MAX=10 )
 add_definitions( -DGTEST_HAS_PTHREAD=0 )
 if ( DDL_DEBUG_OUTPUT )
     add_definitions( -DDDL_DEBUG_HEADER_NAME)
 endif()
 
-INCLUDE_DIRECTORIES(
-    ./
-    include/
-    contrib/gtest-1.7.0/include
-    contrib/gtest-1.7.0/
-)
-
-link_directories(
-    ${CMAKE_HOME_DIRECTORY}/lib
-)
-
-SET( CMAKE_MODULE_PATH  ${CMAKE_MODULE_PATH} ${PROJECT_SOURCE_DIR}/cmake )
-SET( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_HOME_DIRECTORY}/lib )
-SET( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_HOME_DIRECTORY}/lib )
-SET( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_HOME_DIRECTORY}/bin )
+list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
+SET( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib )
+SET( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/lib )
+SET( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/bin )
 
-if( WIN32 AND NOT CYGWIN )
+if (MSVC)
   set( CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /EHsc" )  # Force to always compile with W4
   if( CMAKE_CXX_FLAGS MATCHES "/W[0-4]" )
     string( REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}" )
@@ -72,21 +65,16 @@ if (COVERALLS)
 endif()
 
 # Include the doc component.
-FIND_PACKAGE( doxygen )
-IF ( DOXYGEN_FOUND )
+if(DDL_DOCUMENTATION)
+    find_package(Doxygen REQUIRED)
     CONFIGURE_FILE( doc/openddlparser_doc.in doc/doxygenfile @ONLY )
-    ADD_CUSTOM_TARGET( doc ALL ${DOXYGEN_EXECUTABLE} doc/doxygenfile
+    add_custom_target(doc ALL
+        ${DOXYGEN_EXECUTABLE} doc/doxygenfile
         WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
         COMMENT "Generating API documentation with Doxygen" VERBATIM )
-ENDIF ( DOXYGEN_FOUND )
+endif()
 
-SET ( openddl_parser_src
-    code/OpenDDLCommon.cpp
-    code/OpenDDLExport.cpp
-    code/OpenDDLParser.cpp
-    code/OpenDDLStream.cpp
-    code/DDLNode.cpp
-    code/Value.cpp
+SET ( openddlparser_headers
     include/openddlparser/OpenDDLCommon.h
     include/openddlparser/OpenDDLExport.h
     include/openddlparser/OpenDDLParser.h
@@ -95,76 +83,129 @@ SET ( openddl_parser_src
     include/openddlparser/DDLNode.h
     include/openddlparser/Value.h
     include/openddlparser/TPoolAllocator.h
-    README.md
-)
- 
-SOURCE_GROUP( code            FILES ${openddl_parser_src} )
-
-if ( DDL_STATIC_LIBRARY )
-	ADD_LIBRARY( openddl_parser STATIC
-		${openddl_parser_src}
-	)
-else()
-	ADD_LIBRARY( openddl_parser SHARED
-		${openddl_parser_src}
-	)
-endif()
-
-SET ( GTEST_PATH contrib/gtest-1.7.0 )
-
-SET ( gtest_src
-    ${GTEST_PATH}/src/gtest-death-test.cc
-    ${GTEST_PATH}/src/gtest-filepath.cc
-    ${GTEST_PATH}/src/gtest-internal-inl.h
-    ${GTEST_PATH}/src/gtest-port.cc
-    ${GTEST_PATH}/src/gtest-printers.cc
-    ${GTEST_PATH}/src/gtest-test-part.cc
-    ${GTEST_PATH}/src/gtest-typed-test.cc
-    ${GTEST_PATH}/src/gtest.cc
 )
-
-SET( openddl_parser_unittest_src  
-    test/UnitTestCommon.h
-    test/DDLNodeTest.cpp
-    test/OpenDDLCommonTest.cpp
-    test/OpenDDLExportTest.cpp
-    test/OpenDDLParserTest.cpp
-    test/OpenDDLParserUtilsTest.cpp
-    test/OpenDDLStreamTest.cpp
-    test/OpenDDLIntegrationTest.cpp
-    test/ValueTest.cpp
-    test/OpenDDLDefectsTest.cpp
-	test/OssFuzzTest.cpp
-    test/main.cpp
+SET ( openddlparser_src
+    code/OpenDDLCommon.cpp
+    code/OpenDDLExport.cpp
+    code/OpenDDLParser.cpp
+    code/OpenDDLStream.cpp
+    code/DDLNode.cpp
+    code/Value.cpp
 )
-add_definitions(-DOPENDDL_TEST_DATA="${CMAKE_CURRENT_LIST_DIR}/test/TestData")
-
-SOURCE_GROUP( code  FILES ${openddl_parser_unittest_src} )
-SOURCE_GROUP( gtest FILES ${gtest_src} )
 
-ADD_EXECUTABLE( openddl_parser_unittest 
-    ${gtest_src}
-    ${openddl_parser_unittest_src}
-)
+SOURCE_GROUP( code            FILES ${openddlparser_src} )
 
-target_link_libraries( openddl_parser_unittest openddl_parser ${CMAKE_THREAD_LIBS_INIT} )
+ADD_LIBRARY( openddlparser ${openddlparser_src})
 
-SET( openddl_parser_demo_src  
-    demo/main.cpp 
-)
+target_include_directories(openddlparser PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>)
 
-if (COVERALLS)
-    set(COVERAGE_SRCS     ${gtest_src} ${openddl_parser_unittest_src} )
+target_compile_features(openddlparser PUBLIC cxx_std_11)
 
-    # Create the coveralls target.
-    coveralls_setup(
-        "${COVERAGE_SRCS}" # The source files.
-        ON                 # If we should upload.
-        "${PROJECT_SOURCE_DIR}/cmake/") # (Optional) Alternate project cmake module path.
+if(DDL_BUILD_SHARED_LIBS)
+    set_target_properties(openddlparser PROPERTIES
+        CXX_VISIBILITY_PRESET hidden
+        VISIBILITY_INLINES_HIDDEN TRUE
+    )
+else()
+    target_compile_definitions(openddlparser PUBLIC OPENDDL_STATIC_LIBARY)
 endif()
 
-ADD_EXECUTABLE( openddl_parser_demo
-    ${openddl_parser_demo_src}
-) 
-
-target_link_libraries( openddl_parser_demo openddl_parser )
+set_target_properties( openddlparser PROPERTIES PUBLIC_HEADER "${openddlparser_headers}")
+
+if (DDL_BUILD_TESTS)
+    find_package(Threads REQUIRED)
+
+    SET ( GTEST_PATH contrib/gtest-1.7.0 )
+
+    SET ( gtest_src
+        ${GTEST_PATH}/src/gtest-death-test.cc
+        ${GTEST_PATH}/src/gtest-filepath.cc
+        ${GTEST_PATH}/src/gtest-port.cc
+        ${GTEST_PATH}/src/gtest-printers.cc
+        ${GTEST_PATH}/src/gtest-test-part.cc
+        ${GTEST_PATH}/src/gtest-typed-test.cc
+        ${GTEST_PATH}/src/gtest.cc
+    )
+
+    SET( openddlparser_unittest_src
+        test/DDLNodeTest.cpp
+        test/OpenDDLCommonTest.cpp
+        test/OpenDDLExportTest.cpp
+        test/OpenDDLParserTest.cpp
+        test/OpenDDLParserUtilsTest.cpp
+        test/OpenDDLStreamTest.cpp
+        test/OpenDDLIntegrationTest.cpp
+        test/ValueTest.cpp
+        test/OpenDDLDefectsTest.cpp
+        test/OssFuzzTest.cpp
+        test/main.cpp
+    )
+
+    SOURCE_GROUP( code  FILES ${openddlparser_unittest_src} )
+    SOURCE_GROUP( gtest FILES ${gtest_src} )
+
+    ADD_EXECUTABLE( openddlparser_unittest
+        ${gtest_src}
+        ${openddlparser_unittest_src}
+    )
+
+    target_include_directories(openddlparser_unittest
+        PRIVATE
+            ${GTEST_PATH}
+            ${GTEST_PATH}/include
+    )
+    target_link_libraries(openddlparser_unittest openddlparser Threads::Threads)
+    target_compile_features(openddlparser_unittest PRIVATE cxx_std_11)
+    target_compile_definitions(openddlparser_unittest PRIVATE OPENDDL_TEST_DATA="${PROJECT_SOURCE_DIR}/test/TestData")
+endif ()
+
+if (DDL_BUILD_PARSER_DEMO)
+    SET( openddlparser_demo_src
+        demo/main.cpp
+    )
+
+    if (COVERALLS)
+        set(COVERAGE_SRCS     ${gtest_src} ${openddlparser_unittest_src} )
+
+        # Create the coveralls target.
+        coveralls_setup(
+            "${COVERAGE_SRCS}" # The source files.
+            ON                 # If we should upload.
+            "${PROJECT_SOURCE_DIR}/cmake/") # (Optional) Alternate project cmake module path.
+    endif()
+
+    ADD_EXECUTABLE( openddlparser_demo
+        ${openddlparser_demo_src}
+    )
+
+    target_link_libraries( openddlparser_demo openddlparser )
+    target_compile_features(openddlparser_demo PRIVATE cxx_std_11)
+endif ()
+
+include(GNUInstallDirs)
+include(CMakePackageConfigHelpers)
+
+install(TARGETS openddlparser
+        EXPORT  openddlparser-targets
+        RUNTIME       DESTINATION "${CMAKE_INSTALL_BINDIR}"
+        LIBRARY       DESTINATION "${CMAKE_INSTALL_LIBDIR}"
+        ARCHIVE       DESTINATION "${CMAKE_INSTALL_LIBDIR}"
+        INCLUDES      DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}"
+        PUBLIC_HEADER DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/openddlparser")
+
+export(EXPORT openddlparser-targets
+       FILE openddlparser-config.cmake
+       NAMESPACE openddlparser::)
+
+install(EXPORT openddlparser-targets
+    FILE openddlparser-config.cmake
+    NAMESPACE openddlparser::
+    DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/openddlparser")
+
+write_basic_package_version_file(
+    "${CMAKE_BINARY_DIR}/openddlparser-config-version.cmake"
+    COMPATIBILITY SameMajorVersion)
+
+install(
+    FILES "${CMAKE_BINARY_DIR}/openddlparser-config-version.cmake"
+    DESTINATION "${CMAKE_INSTALL_LIBDIR}/cmake/openddlparser")