Commit 0dd9fc13 authored by Reinhard Biegel's avatar Reinhard Biegel
Browse files

mod(CMake): Reworked OSI linkage handling



In CMakeLists.txt, add_openpass_target() now supports a parameter to
`LINKOSI`, specifying shared or static linking of OSI and protobuf.
Signed-off-by: Reinhard Biegel's avatarReinhard Biegel <reinhard.biegel@in-tech.com>
parent 0316819e
################################################################################ ################################################################################
# Copyright (c) 2021 Bayerische Motoren Werke Aktiengesellschaft (BMW AG) # Copyright (c) 2021 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
# #
# This program and the accompanying materials are made available under the # This program and the accompanying materials are made
# terms of the Eclipse Public License 2.0 which is available at # available under the terms of the Eclipse Public License 2.0
# http://www.eclipse.org/legal/epl-2.0. # which is available at https://www.eclipse.org/legal/epl-2.0/
# #
# SPDX-License-Identifier: EPL-2.0 # SPDX-License-Identifier: EPL-2.0
################################################################################ ################################################################################
# #
# - Find OSI # Find Package Adapter for OSI (Open Simulation Interface)
# Find the OSI includes and library
# #
# OSI_INCLUDE_DIR - Where to find OSI includes # Creates the follwoing imported targets (if available):
# OSI_LIBRARIES - List of libraries when using OSI # - osi::shared
# OSI_FOUND - True if OSI was found # - osi::static
# - osi::pic
IF(OSI_INCLUDE_DIR) set(OSI_SHARED_NAMES
SET(OSI_FIND_QUIETLY TRUE) open_simulation_interface.lib
ENDIF(OSI_INCLUDE_DIR) libopen_simulation_interface.dll.a
libopen_simulation_interface.so
FIND_PATH(OSI_INCLUDE_DIR "osi3/osi_version.pb.h"
PATHS
${PREFIX_PATH}
$ENV{OSI_HOME}/include
$ENV{EXTERNLIBS}/OSI/include
/usr/local/include
/usr/include
PATH_SUFFIXES include
DOC "OSI - Headers"
) )
SET(OSI_NAMES set(OSI_STATIC_NAMES
osi3/open_simulation_interface.lib open_simulation_interface_static.lib
osi3/libopen_simulation_interface.dll.a libopen_simulation_interface_static.a
osi3/libopen_simulation_interface_pic.lib
osi3/libopen_simulation_interface.so
) )
SET(OSI_DBG_NAMES set(OSI_PIC_NAMES
osi3/open_simulation_interfaced.lib open_simulation_interface_pic.lib
osi3/libopen_simulation_interfaced.dll.a libopen_simulation_interface_pic.a
osi3/libopen_simulation_interface_picd.lib
) )
FIND_LIBRARY(OSI_LIBRARY NAMES ${OSI_NAMES} find_library(OSI_SHARED_LIBRARY NAMES ${OSI_SHARED_NAMES}
PATHS PATHS
${PREFIX_PATH} ${PREFIX_PATH}
$ENV{OSI_HOME} /usr/local
$ENV{EXTERNLIBS}/OSI /usr
/usr/local PATH_SUFFIXES
/usr lib/osi3
PATH_SUFFIXES lib lib64 lib
DOC "OSI - Library" lib64
) )
INCLUDE(FindPackageHandleStandardArgs) find_library(OSI_STATIC_LIBRARY NAMES ${OSI_STATIC_NAMES}
PATHS
${PREFIX_PATH}
/usr/local
/usr
PATH_SUFFIXES
lib/osi3
lib
lib64
)
IF(MSVC) find_library(OSI_PIC_LIBRARY NAMES ${OSI_PIC_NAMES}
# VisualStudio needs a debug version PATHS
FIND_LIBRARY(OSI_LIBRARY_DEBUG NAMES ${OSI_DBG_NAMES}
PATHS
${PREFIX_PATH} ${PREFIX_PATH}
$ENV{OSI_HOME}/lib /usr/local
$ENV{EXTERNLIBS}/OSI/lib /usr
DOC "OSI - Library (Debug)" PATH_SUFFIXES
) lib/osi3
lib
lib64
)
if(OSI_SHARED_LIBRARY)
message(STATUS "Found OSI (shared): ${OSI_SHARED_LIBRARY}")
IF(OSI_LIBRARY_DEBUG AND OSI_LIBRARY) get_filename_component(OSI_SHARED_LIBRARY_DIR "${OSI_SHARED_LIBRARY}" DIRECTORY)
SET(OSI_LIBRARIES optimized ${OSI_LIBRARY} debug ${OSI_LIBRARY_DEBUG})
ENDIF(OSI_LIBRARY_DEBUG AND OSI_LIBRARY)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(OSI DEFAULT_MSG OSI_LIBRARY OSI_LIBRARY_DEBUG OSI_INCLUDE_DIR) add_library(osi::shared IMPORTED SHARED)
set_target_properties(osi::shared
PROPERTIES
IMPORTED_LOCATION ${OSI_SHARED_LIBRARY}
IMPORTED_IMPLIB ${OSI_SHARED_LIBRARY}
INTERFACE_INCLUDE_DIRECTORIES ${OSI_SHARED_LIBRARY_DIR}/../../include
INTERFACE_LINK_LIBRARIES protobuf::libprotobuf)
else()
message(STATUS "Didn't find OSI (shared)")
endif()
MARK_AS_ADVANCED(OSI_LIBRARY OSI_LIBRARY_DEBUG OSI_INCLUDE_DIR) if(OSI_STATIC_LIBRARY)
message(STATUS "Found OSI (static): ${OSI_STATIC_LIBRARY}")
ELSE(MSVC) get_filename_component(OSI_STATIC_LIBRARY_DIR "${OSI_STATIC_LIBRARY}" DIRECTORY)
# rest of the world add_library(osi::static IMPORTED STATIC)
SET(OSI_LIBRARIES ${OSI_LIBRARY}) set_target_properties(osi::static
PROPERTIES
IMPORTED_LOCATION ${OSI_STATIC_LIBRARY}
INTERFACE_INCLUDE_DIRECTORIES ${OSI_STATIC_LIBRARY_DIR}/../../include
INTERFACE_LINK_LIBRARIES protobuf::libprotobuf_static)
else()
message(STATUS "Didn't find OSI (static)")
endif()
FIND_PACKAGE_HANDLE_STANDARD_ARGS(OSI DEFAULT_MSG OSI_LIBRARY OSI_INCLUDE_DIR) if(OSI_PIC_LIBRARY)
message(STATUS "Found OSI (pic): ${OSI_PIC_LIBRARY}")
MARK_AS_ADVANCED(OSI_LIBRARY OSI_INCLUDE_DIR) get_filename_component(OSI_PIC_LIBRARY_DIR "${OSI_PIC_LIBRARY}" DIRECTORY)
add_library(osi::pic IMPORTED STATIC)
ENDIF(MSVC) set_target_properties(osi::pic
PROPERTIES
IMPORTED_LOCATION ${OSI_PIC_LIBRARY}
INTERFACE_INCLUDE_DIRECTORIES ${OSI_PIC_LIBRARY_DIR}/../../include
INTERFACE_LINK_LIBRARIES protobuf::libprotobuf_static)
else()
message(STATUS "Didn't find OSI (pic)")
endif()
unset(OSI_SHARED_LIBRARY)
unset(OSI_STATIC_LIBRARY)
unset(OSI_PIC_LIBRARY)
################################################################################
# Copyright (c) 2021 Bayerische Motoren Werke Aktiengesellschaft (BMW AG)
#
# This program and the accompanying materials are made
# available under the terms of the Eclipse Public License 2.0
# which is available at https://www.eclipse.org/legal/epl-2.0/
#
# SPDX-License-Identifier: EPL-2.0
################################################################################
#
# find_package adapter for protobuf
#
# Original protpbuf CMake Config file doesn't provide static targets.
#
# Creates the follwoing imported targets (if available):
# - protobuf::libprotobuf
# - protobuf::libprotobuf_static
set(PROTOBUF_SHARED_NAMES
protobuf.lib
libprotobuf.dll.a
libprotobuf.so
)
set(PROTOBUF_STATIC_NAMES
protobuf_static.lib
libprotobuf.a
)
find_library(PROTOBUF_SHARED_LIBRARY NAMES ${PROTOBUF_SHARED_NAMES}
PATHS
${PREFIX_PATH}
/usr/local
/usr
PATH_SUFFIXES
lib
lib64
)
find_library(PROTOBUF_STATIC_LIBRARY NAMES ${PROTOBUF_STATIC_NAMES}
PATHS
${PREFIX_PATH}
/usr/local
/usr
PATH_SUFFIXES
lib
lib64
)
if(PROTOBUF_SHARED_LIBRARY)
message(STATUS "Found protobuf (shared): ${PROTOBUF_SHARED_LIBRARY}")
get_filename_component(PROTOBUF_SHARED_LIBRARY_DIR "${PROTOBUF_SHARED_LIBRARY}" DIRECTORY)
add_library(protobuf::libprotobuf IMPORTED SHARED)
set_target_properties(protobuf::libprotobuf
PROPERTIES
IMPORTED_LOCATION ${PROTOBUF_SHARED_LIBRARY}
IMPORTED_IMPLIB ${PROTOBUF_SHARED_LIBRARY}
INTERFACE_COMPILE_DEFINITIONS PROTOBUF_USE_DLLS
INTERFACE_INCLUDE_DIRECTORIES ${PROTOBUF_SHARED_LIBRARY_DIR}/../include
INTERFACE_LINK_LIBRARIES pthread)
else()
message(STATUS "Didn't find protobuf (shared)")
endif()
if(PROTOBUF_STATIC_LIBRARY)
message(STATUS "Found protobuf (static): ${PROTOBUF_STATIC_LIBRARY}")
get_filename_component(PROTOBUF_STATIC_LIBRARY_DIR "${PROTOBUF_STATIC_LIBRARY}" DIRECTORY)
add_library(protobuf::libprotobuf_static IMPORTED STATIC)
set_target_properties(protobuf::libprotobuf_static
PROPERTIES
IMPORTED_LOCATION ${PROTOBUF_STATIC_LIBRARY}
INTERFACE_INCLUDE_DIRECTORIES ${PROTOBUF_STATIC_LIBRARY_DIR}/../include
INTERFACE_LINK_LIBRARIES pthread)
else()
message(STATUS "Didn't find protobuf (static)")
endif()
unset(PROTOBUF_SHARED_LIBRARY)
unset(PROTOBUF_STATIC_LIBRARY)
...@@ -81,7 +81,7 @@ endif() ...@@ -81,7 +81,7 @@ endif()
# [INCDIRS <include-directories>] # [INCDIRS <include-directories>]
# [LIBRARIES <libraries>] # [LIBRARIES <libraries>]
# [UIS <qt_uis>] # [UIS <qt_uis>]
# [LINKOSI]) # [LINKOSI [shared|static]]
# [LINKGUI] # [LINKGUI]
# [FOLDER <category>] # [FOLDER <category>]
# [COMPONENT <gui|sim|core|bin|module>]) # [COMPONENT <gui|sim|core|bin|module>])
...@@ -94,7 +94,7 @@ endif() ...@@ -94,7 +94,7 @@ endif()
# [INCDIRS <include-directories>] # [INCDIRS <include-directories>]
# [LIBRARIES <libraries>] # [LIBRARIES <libraries>]
# [UIS <qt_uis>] # [UIS <qt_uis>]
# [LINKOSI] # [LINKOSI [shared|static]]
# [LINKGUI] # [LINKGUI]
# [FOLDER <category>] # [FOLDER <category>]
# [COMPONENT <gui|sim|core|bin|module>]) # [COMPONENT <gui|sim|core|bin|module>])
...@@ -106,7 +106,7 @@ endif() ...@@ -106,7 +106,7 @@ endif()
# [INCDIRS <include-directories>] # [INCDIRS <include-directories>]
# [LIBRARIES <libraries>] # [LIBRARIES <libraries>]
# [UIS <qt_uis>] # [UIS <qt_uis>]
# [LINKOSI] # [LINKOSI [shared|static]]
# [LINKGUI] # [LINKGUI]
# [DEFAULT_MAIN] # [DEFAULT_MAIN]
# [SIMCORE_DEPS <dependencies>] # [SIMCORE_DEPS <dependencies>]
...@@ -122,7 +122,7 @@ endif() ...@@ -122,7 +122,7 @@ endif()
# UIS Qt UI files # UIS Qt UI files
# INCDIRS Additional include directories # INCDIRS Additional include directories
# LIBRARIES Additional libraries to link # LIBRARIES Additional libraries to link
# LINKOSI Shortcut for adding OSI include directories and libraries (incl. protobuf) # LINKOSI Shortcut for adding OSI include directories and libraries (incl. protobuf) as 'static' or 'shared' (default).
# LINKGUI Shortcut for adding GUI Libraries # LINKGUI Shortcut for adding GUI Libraries
# DEFAULT_MAIN Links a simple main() implementation for running GTest # DEFAULT_MAIN Links a simple main() implementation for running GTest
# SIMCORE_DEPS Adds dependencies on simulation core targets to a test # SIMCORE_DEPS Adds dependencies on simulation core targets to a test
...@@ -137,12 +137,13 @@ endif() ...@@ -137,12 +137,13 @@ endif()
# - gtest/gmock/pthread libraries are linked # - gtest/gmock/pthread libraries are linked
# - Tests are excluded form the 'all' target # - Tests are excluded form the 'all' target
# - If DEFAULT_MAIN argument is provided, adds '--default-xml' to test executable command line arguments # - If DEFAULT_MAIN argument is provided, adds '--default-xml' to test executable command line arguments
# - PATH and LD_LIBRARY_PATH are set under Windows and Linux, respectively, so that test executables can resolve run-time dependencies.
# - General: # - General:
# - Target properties PROJECT_LABEL and OUTPUT_NAME are set to the target's name # - Target properties PROJECT_LABEL and OUTPUT_NAME are set to the target's name
# - Target property DEBUG_POSTFIX is set to CMAKE_DEBUG_POSTFIX # - Target property DEBUG_POSTFIX is set to CMAKE_DEBUG_POSTFIX
## ##
function(add_openpass_target) function(add_openpass_target)
cmake_parse_arguments(PARSED_ARG "LINKGUI;LINKOSI;DEFAULT_MAIN" "NAME;TYPE;LINKAGE" "HEADERS;SOURCES;INCDIRS;LIBRARIES;UIS;SIMCORE_DEPS;RESOURCES;FOLDER;COMPONENT" ${ARGN}) cmake_parse_arguments(PARSED_ARG "LINKGUI;DEFAULT_MAIN" "NAME;TYPE;LINKAGE;LINKOSI" "HEADERS;SOURCES;INCDIRS;LIBRARIES;UIS;SIMCORE_DEPS;RESOURCES;FOLDER;COMPONENT" ${ARGN})
if(TARGET ${PARSED_ARG_NAME}) if(TARGET ${PARSED_ARG_NAME})
message(STATUS "Target '${PARSED_ARG_NAME}' already defined. Skipping.") message(STATUS "Target '${PARSED_ARG_NAME}' already defined. Skipping.")
...@@ -274,16 +275,26 @@ function(add_openpass_target) ...@@ -274,16 +275,26 @@ function(add_openpass_target)
Boost::headers Boost::headers
) )
if(${PARSED_ARG_LINKOSI}) # LINKOSI handling
target_include_directories(${PARSED_ARG_NAME} PRIVATE
${OSI_INCLUDE_DIR}
protobuf::libprotobuf
)
target_link_libraries(${PARSED_ARG_NAME} # fallback to default if value is omitted
${OSI_LIBRARIES} if("LINKOSI" IN_LIST PARSED_ARG_KEYWORDS_MISSING_VALUES)
protobuf::libprotobuf set(PARSED_ARG_LINKOSI "shared")
) endif()
if(DEFINED PARSED_ARG_LINKOSI)
# validate value
set(VALID_LINKOSI_VALUES "" "shared" "static")
if(NOT "${PARSED_ARG_LINKOSI}" IN_LIST VALID_LINKOSI_VALUES)
message(FATAL_ERROR "Invalid value for LINKOSI. Supported settings are '', 'shared' and 'static'")
endif()
# replace static with pic for library targets
if("${PARSED_ARG_TYPE}" STREQUAL "library" AND "${PARSED_ARG_LINKOSI}" STREQUAL "static")
set(PARSED_ARG_LINKOSI "pic")
endif()
target_link_libraries(${PARSED_ARG_NAME} osi::${PARSED_ARG_LINKOSI})
endif() endif()
target_compile_options(${PARSED_ARG_NAME} PRIVATE target_compile_options(${PARSED_ARG_NAME} PRIVATE
...@@ -305,12 +316,13 @@ function(add_openpass_target) ...@@ -305,12 +316,13 @@ function(add_openpass_target)
if(DEFINED PARSED_ARG_LIBRARIES) if(DEFINED PARSED_ARG_LIBRARIES)
list(APPEND DEPS ${PARSED_ARG_LIBRARIES}) list(APPEND DEPS ${PARSED_ARG_LIBRARIES})
endif() endif()
if(${PARSED_ARG_LINKOSI}) if(DEFINED PARSED_ARG_LINKOSI)
list(APPEND DEPS "${OSI_LIBRARIES}" protobuf::libprotobuf) list(APPEND DEPS osi::${PARSED_ARG_LINKOSI} protobuf::libprotobuf)
endif() endif()
message(DEBUG "Locating shared library test dependencies...") message(DEBUG "Locating shared library test dependencies for ${PARSED_ARG_NAME}")
foreach(DEP IN LISTS DEPS) foreach(DEP IN LISTS DEPS)
message(DEBUG "Locating ${DEP}...")
if(TARGET ${DEP}) if(TARGET ${DEP})
set(DEP_PATH "") set(DEP_PATH "")
message(DEBUG "Target dependency: ${DEP}") message(DEBUG "Target dependency: ${DEP}")
...@@ -386,7 +398,15 @@ function(add_openpass_target) ...@@ -386,7 +398,15 @@ function(add_openpass_target)
list(APPEND DEP_PATHS "${DEP_PATH}") list(APPEND DEP_PATHS "${DEP_PATH}")
endforeach() endforeach()
list(REMOVE_DUPLICATES DEP_PATHS)
if(WIN32) if(WIN32)
# try to move MSYS system folder to the end of the list
set(DEP_PATHS_NO_MSYS ${DEP_PATHS})
list(FILTER DEP_PATHS EXCLUDE REGEX "msys")
list(FILTER DEP_PATHS_MSYS INCLUDE REGEX "msys")
list(APPEND DEP_PATHS ${DEP_PATHS_MSYS})
list(JOIN DEP_PATHS "\\;" ADDITIONAL_PATHS) list(JOIN DEP_PATHS "\\;" ADDITIONAL_PATHS)
set(CURRENT_PATH "$ENV{PATH}") set(CURRENT_PATH "$ENV{PATH}")
string(REGEX REPLACE "\;" "\\\;" CURRENT_PATH "${CURRENT_PATH}") string(REGEX REPLACE "\;" "\\\;" CURRENT_PATH "${CURRENT_PATH}")
......
...@@ -79,7 +79,6 @@ set_property(GLOBAL PROPERTY USE_FOLDERS ON) ...@@ -79,7 +79,6 @@ set_property(GLOBAL PROPERTY USE_FOLDERS ON)
if(WITH_SIMCORE OR WITH_TESTS) if(WITH_SIMCORE OR WITH_TESTS)
find_package(Protobuf REQUIRED) find_package(Protobuf REQUIRED)
add_compile_definitions(PROTOBUF_USE_DLLS)
find_package(OSI REQUIRED) find_package(OSI REQUIRED)
......
...@@ -42,13 +42,12 @@ add_openpass_target( ...@@ -42,13 +42,12 @@ add_openpass_target(
${FMILibrary_INCLUDE_DIR}/JM ${FMILibrary_INCLUDE_DIR}/JM
src/FmiImporter/include src/FmiImporter/include
../../core/opSimulation/modules/World_OSI ../../core/opSimulation/modules/World_OSI
${OSI_INCLUDE_DIR}
LIBRARIES LIBRARIES
Qt5::Core Qt5::Core
${FMILibrary_LIBRARY_DIR} ${FMILibrary_LIBRARY_DIR}
Common Common
CoreCommon CoreCommon
protobuf::libprotobuf
-L${OSI_INCLUDE_DIR}/../lib/osi3 open_simulation_interface_pic LINKOSI static
) )
...@@ -40,13 +40,12 @@ add_openpass_target( ...@@ -40,13 +40,12 @@ add_openpass_target(
${FMILibrary_INCLUDE_DIR}/FMI1 ${FMILibrary_INCLUDE_DIR}/FMI1
${FMILibrary_INCLUDE_DIR}/FMI2 ${FMILibrary_INCLUDE_DIR}/FMI2
${FMILibrary_INCLUDE_DIR}/JM ${FMILibrary_INCLUDE_DIR}/JM
${OSI_INCLUDE_DIR}
LIBRARIES LIBRARIES
Qt5::Core Qt5::Core
Common Common
${FMILibrary_LIBRARY_DIR} ${FMILibrary_LIBRARY_DIR}
protobuf::libprotobuf
-L${OSI_INCLUDE_DIR}/../lib/osi3 open_simulation_interface_pic LINKOSI static
) )
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment