Skip to content

Commit

Permalink
Remove hard coded platform specific link libraries from setup.py.
Browse files Browse the repository at this point in the history
They are now automatically propagated. This avoids a potential loss of sync between the link libraries in CMake scripts and those in `setup.py`.
  • Loading branch information
bcoconni committed Feb 5, 2022
1 parent cba9ea0 commit 5e967a4
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 50 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ find_package(PkgConfig)

set(CMAKE_POSITION_INDEPENDENT_CODE ON)
add_subdirectory(src)
get_target_property(libJSBSim_LINK_LIBRARIES libJSBSim LINK_LIBRARIES)

################################################################################
# Documentation #
Expand Down Expand Up @@ -123,6 +122,7 @@ endif(CXXTEST_FOUND)

if (PKG_CONFIG_FOUND)
if (NOT BUILD_SHARED_LIBS)
get_target_property(libJSBSim_LINK_LIBRARIES libJSBSim LINK_LIBRARIES)
foreach(_LIB ${libJSBSim_LINK_LIBRARIES})
set(JSBSIM_PKG_CONFIG_LINK_LIBRARIES "${JSBSIM_PKG_CONFIG_LINK_LIBRARIES} -l${_LIB}")
endforeach(_LIB)
Expand Down
11 changes: 1 addition & 10 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -84,19 +84,10 @@ endforeach(OBJECT)
foreach(_FILE ${SOURCE_FILES})
get_filename_component(FILE_EXTENSION ${_FILE} EXT)
if((NOT ${FILE_EXTENSION} STREQUAL ".h") AND (NOT ${FILE_EXTENSION} STREQUAL ".hxx") AND (NOT ${FILE_EXTENSION} STREQUAL ".hpp"))
string(APPEND JSBSIM_SOURCE_FILES "'${_FILE}',")
list(APPEND JSBSIM_SOURCE_FILES ${_FILE})
endif()
endforeach(_FILE)

foreach(_LIB ${libJSBSim_LINK_LIBRARIES})
string(REGEX MATCH "(^[A-Za-z0-9_]+).lib" LIBNAME ${_LIB})
if (LIBNAME STREQUAL "")
string(APPEND JSBSIM_LINK_LIBRARIES "'${_LIB}',")
else()
string(APPEND JSBSIM_LINK_LIBRARIES "'${CMAKE_MATCH_1}',")
endif()
endforeach(_LIB)

# BUILD_ROOT_PATH is used by setup.py to determine if the library has already
# been built.
file(RELATIVE_PATH BUILD_ROOT_PATH ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_BINARY_DIR})
Expand Down
34 changes: 20 additions & 14 deletions python/setup.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -129,23 +129,28 @@ else:

compiler = new_compiler(compiler=compiler_name)

if compiler.compiler_type == 'unix':
cpp_compile_flag = ['-std=c++11']
else:
cpp_compile_flag = []

if compiler.compiler_type == 'msvc':
link_libraries = ['wsock32', 'ws2_32']
else:
link_libraries = [${JSBSIM_LINK_LIBRARIES}]

# Update the JSBSim library path according to the --config option
if args.config:
if compiler.compiler_type == 'msvc':
library_path=os.path.join(library_path, args.config)
else:
raise ArgumentError(None, 'option --config not recognized.')

convert_CMake_list_to_Python_list = lambda s: [item.strip() for item in s.split(';') if item!='']

if compiler.compiler_type == 'unix':
cpp_compile_flag = ['-std=c++11']
cpp_link_flags = []
link_libraries = convert_CMake_list_to_Python_list('${JSBSIM_LINK_LIBRARIES};${JSBSIM_UNIX_LINK_LIBRARIES}')
elif compiler.compiler_type == 'msvc':
cpp_compile_flag = []
cpp_link_flags = []
link_libraries = convert_CMake_list_to_Python_list('${JSBSIM_LINK_LIBRARIES};${JSBSIM_WINDOWS_LINK_LIBRARIES}')
else:
cpp_compile_flag = []
cpp_link_flags = []
link_libraries = convert_CMake_list_to_Python_list('${JSBSIM_LINK_LIBRARIES}')

# Check if the library exists and build the Python module accordingly.
if 'sdist' not in dist.commands and compiler.find_library_file([library_path],
'JSBSim'):
Expand All @@ -155,20 +160,21 @@ if 'sdist' not in dist.commands and compiler.find_library_file([library_path],
'include_dirs': ['src'],
'libraries': ['JSBSim'] + link_libraries,
'library_dirs': [library_path],
'extra_compile_args': cpp_compile_flag }
'extra_compile_args': cpp_compile_flag,
'extra_link_args': cpp_link_flags }
setup_kwargs = { 'cmdclass' : {'build_ext': QuietBuild,
'install_lib': InstallJSBSimModule}}
else:
# We cannot find the JSBSim library so the Python module must be built from
# the sources.
if compiler.compiler_type == 'msvc':
compile_flags = ['/D'+flag for flag in '${JSBSIM_MSVC_COMPILE_DEFINITIONS};${JSBSIM_COMPILE_DEFINITIONS}'.split(';')]
compile_flags = ['/D'+flag for flag in convert_CMake_list_to_Python_list('${JSBSIM_MSVC_COMPILE_DEFINITIONS};${JSBSIM_COMPILE_DEFINITIONS}')]
if sys.version_info < (3,9):
compile_flags = [flag.replace('"', '\\"') for flag in compile_flags]
else:
compile_flags = ['-D'+flag for flag in '${JSBSIM_COMPILE_DEFINITIONS}'.split(';')]
compile_flags = ['-D'+flag for flag in convert_CMake_list_to_Python_list('${JSBSIM_COMPILE_DEFINITIONS}')]

ext_kwargs = { 'sources': ['jsbsim.pyx', ${JSBSIM_SOURCE_FILES}],
ext_kwargs = { 'sources': convert_CMake_list_to_Python_list('jsbsim.pyx;${JSBSIM_SOURCE_FILES}'),
'libraries': link_libraries,
'include_dirs': ['src', 'src/simgear/xml'],
'extra_compile_args': compile_flags }
Expand Down
58 changes: 33 additions & 25 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,11 @@ set(MSVC_COMPILE_DEFINITIONS _USE_MATH_DEFINES NOMINMAX)
# Init the list of libraries that JSBSim links with #
################################################################################

if(WIN32 AND (MSVC OR MINGW))
# not applicable to cygwin
if(MSVC)
set(JSBSIM_LINK_LIBRARIES "wsock32.lib" "ws2_32.lib")
else()
set(JSBSIM_LINK_LIBRARIES "-lwsock32 -lws2_32")
endif(MSVC)
elseif(UNIX)
# not applicable to cygwin
set(JSBSIM_LINK_LIBRARIES "m")
else()
set(JSBSIM_LINK_LIBRARIES)
endif()
# MSVC and MINGW linked libraries
set(WINDOWS_LINK_LIBRARIES wsock32 ws2_32)
# Unix linked libraries
set(UNIX_LINK_LIBRARIES m)


################################################################################
# Build and install libraries #
Expand Down Expand Up @@ -53,17 +45,6 @@ add_subdirectory(math)
add_subdirectory(models)
add_subdirectory(simgear)

if(EXPAT_FOUND)
include_directories(${EXPAT_INCLUDE_DIRS})
if (PKG_CONFIG_FOUND)
set(JSBSIM_LINK_LIBRARIES ${PC_EXPAT_LIBRARIES} ${JSBSIM_LINK_LIBRARIES})
else()
set(JSBSIM_LINK_LIBRARIES ${EXPAT_LIBRARIES} ${JSBSIM_LINK_LIBRARIES})
endif()
else()
list(APPEND COMPILE_DEFINITIONS HAVE_EXPAT_CONFIG_H)
endif()

set(HEADERS FGFDMExec.h
FGJSBBase.h)
set(SOURCES FGFDMExec.cpp
Expand All @@ -86,6 +67,20 @@ add_library(libJSBSim ${HEADERS} ${SOURCES}
$<TARGET_OBJECTS:Simgear>
)

if(EXPAT_FOUND)
include_directories(${EXPAT_INCLUDE_DIRS})
if (PKG_CONFIG_FOUND)
target_link_libraries(libJSBSim ${PC_EXPAT_LIBRARIES})
set(ALL_LINK_LIBRARIES ${PC_EXPAT_LIBRARIES})
else()
target_link_libraries(libJSBSim ${EXPAT_LIBRARIES})
set(ALL_LINK_LIBRARIES ${EXPAT_LIBRARIES})
endif()
else()
list(APPEND COMPILE_DEFINITIONS HAVE_EXPAT_CONFIG_H)
endif()

# Manage compile definitions
set(JSBSIM_COMPILE_DEFINITIONS ${COMPILE_DEFINITIONS} PARENT_SCOPE)
set(JSBSIM_MSVC_COMPILE_DEFINITIONS ${MSVC_COMPILE_DEFINITIONS} PARENT_SCOPE)

Expand All @@ -106,7 +101,20 @@ set_target_properties (libJSBSim PROPERTIES
VERSION ${LIBRARY_VERSION}
TARGET_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})

target_link_libraries(libJSBSim ${JSBSIM_LINK_LIBRARIES})
# Manage link libraries
set(JSBSIM_LINK_LIBRARIES ${ALL_LINK_LIBRARIES} PARENT_SCOPE)
set(JSBSIM_WINDOWS_LINK_LIBRARIES ${WINDOWS_LINK_LIBRARIES} PARENT_SCOPE)
set(JSBSIM_UNIX_LINK_LIBRARIES ${UNIX_LINK_LIBRARIES} PARENT_SCOPE)

if(WIN32 AND (MSVC OR MINGW))
foreach(LINK_LIBRARY ${WINDOWS_LINK_LIBRARIES})
target_link_libraries(libJSBSim ${LINK_LIBRARY})
endforeach()
elseif(UNIX)
foreach(LINK_LIBRARY ${UNIX_LINK_LIBRARIES})
target_link_libraries(libJSBSim ${LINK_LIBRARY})
endforeach()
endif()

if(BUILD_SHARED_LIBS)
set_target_properties (libJSBSim PROPERTIES
Expand Down

0 comments on commit 5e967a4

Please sign in to comment.