Skip to content

Commit

Permalink
Make a Python package installable by pip (issue #73)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoconni committed Jan 6, 2019
1 parent 48a7f99 commit 7714ad0
Show file tree
Hide file tree
Showing 19 changed files with 140 additions and 26 deletions.
20 changes: 10 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ if (SYSTEM_EXPAT)
find_package(EXPAT)
endif()

function(get_tail INPUT_STRING OUTPUT_STRING SEPARATOR)
string(REPLACE ${SEPARATOR} " " TEMP_LIST ${INPUT_STRING})
separate_arguments(TEMP_LIST)
list(GET TEMP_LIST -1 TEMP_TAIL)
string(STRIP ${TEMP_TAIL} TAIL)
set(${OUTPUT_STRING} ${TAIL} PARENT_SCOPE)
endfunction()

################################################################################
# Build JSBSim libs and exec #
################################################################################
Expand Down Expand Up @@ -80,14 +88,6 @@ endif()
# Packaging #
################################################################################

function(get_tail INPUT_STRING OUTPUT_STRING)
string(REPLACE "\t" " " TEMP_LIST ${INPUT_STRING})
separate_arguments(TEMP_LIST)
list(GET TEMP_LIST -1 TEMP_TAIL)
string(STRIP ${TEMP_TAIL} TAIL)
set(${OUTPUT_STRING} ${TAIL} PARENT_SCOPE)
endfunction()

# Extract the host architecture for the package name
if (UNIX)
execute_process(COMMAND uname -m OUTPUT_VARIABLE ARCH OUTPUT_STRIP_TRAILING_WHITESPACE)
Expand All @@ -107,8 +107,8 @@ if (UNIX)
else()
execute_process(COMMAND ${LSB_RELEASE_EXECUTABLE} -i OUTPUT_VARIABLE DISTRIB_NAME)
execute_process(COMMAND ${LSB_RELEASE_EXECUTABLE} -r OUTPUT_VARIABLE DISTRIB_RELEASE)
get_tail(${DISTRIB_NAME} LINUX_DISTRIB_NAME)
get_tail(${DISTRIB_RELEASE} LINUX_DISTRIB_RELEASE)
get_tail(${DISTRIB_NAME} LINUX_DISTRIB_NAME "\t")
get_tail(${DISTRIB_RELEASE} LINUX_DISTRIB_RELEASE "\t")
string(TOLOWER ${LINUX_DISTRIB_NAME} TEMP_NAME)
if (TEMP_NAME STREQUAL "fedora")
set(LINUX_DISTRIB_NAME ".fc")
Expand Down
49 changes: 48 additions & 1 deletion python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ set_source_files_properties(jsbsim.pyx PROPERTIES CYTHON_IS_CXX TRUE)

# Build the Python module using Cython and the JSBSim library
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
compile_pyx(jsbsim JSBSIM_CXX jsbsim.pyx)
compile_pyx(jsbsim _JSBSIM_CXX jsbsim.pyx)
file(RELATIVE_PATH JSBSIM_CXX "${CMAKE_CURRENT_BINARY_DIR}" ${_JSBSIM_CXX})

execute_process(COMMAND ${PYTHON_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/findModuleFileName.py OUTPUT_VARIABLE PYTHON_MODULE_NAME)
set(JSBSIM_PYTHON_MODULE "${CMAKE_BINARY_DIR}/tests/${PYTHON_MODULE_NAME}")
Expand All @@ -17,19 +18,65 @@ if(MSVC)
if(NOT (_GENERATOR STREQUAL NINJA))
set(USING_MSBUILD 1)
endif()
set(COPY_COMMAND "copy")
else()
set(CXX_FLAGS "'-std=c++11'")
set(COPY_COMMAND "cp")
endif(MSVC)

set(SETUP_PY "${CMAKE_CURRENT_BINARY_DIR}/setup.py")
configure_file(setup.py.in ${SETUP_PY})

# Duplicate the files for the source distribution of JSBSim
cmake_policy(SET CMP0051 NEW)
get_target_property(libJSBSim_SOURCE_FILES libJSBSim SOURCES)
get_target_property(libJSBSim_DIRECTORY libJSBSim TARGET_DIRECTORY)
file(RELATIVE_PATH libJSBSim_PATH ${CMAKE_SOURCE_DIR} ${libJSBSim_DIRECTORY})
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/MANIFEST.in" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/package")
file(COPY "${CMAKE_SOURCE_DIR}/src/simgear/xml/xmltok_impl.c"
DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/package/src/simgear/xml")
file(COPY "${CMAKE_SOURCE_DIR}/src/simgear/xml/xmltok_ns.c"
DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/package/src/simgear/xml")
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/jsbsim.pyx" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/package")
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/jsbsim.pxd" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/package")
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/ExceptionManagement.h" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/package")
file(COPY "${CMAKE_CURRENT_SOURCE_DIR}/fpectlmodule.h" DESTINATION "${CMAKE_CURRENT_BINARY_DIR}/package")
foreach(OBJECT ${libJSBSim_SOURCE_FILES})
string(SUBSTRING ${OBJECT} 0 17 HEADER)
if (${HEADER} STREQUAL "$<TARGET_OBJECTS:")
string(LENGTH ${OBJECT} OBJECT_LENGTH)
math(EXPR TARGET_LENGTH "${OBJECT_LENGTH}-18")
string(SUBSTRING ${OBJECT} 17 ${TARGET_LENGTH} TARGET)
get_target_property(TARGET_SOURCE_FILES ${TARGET} SOURCES)
get_target_property(TARGET_SOURCE_DIRECTORY ${TARGET} TARGET_DIRECTORY)
file(RELATIVE_PATH TARGET_PATH ${CMAKE_SOURCE_DIR} "${TARGET_SOURCE_DIRECTORY}")
file(MAKE_DIRECTORY "${TARGET_PATH}")
foreach(_FILE ${TARGET_SOURCE_FILES})
file(COPY "${TARGET_SOURCE_DIRECTORY}/${_FILE}" DESTINATION "package/${TARGET_PATH}")
list(APPEND SOURCE_FILES "${TARGET_PATH}/${_FILE}")
endforeach()
else()
file(COPY "${libJSBSim_DIRECTORY}/${OBJECT}" DESTINATION "package/${libJSBSim_PATH}")
list(APPEND SOURCE_FILES "${libJSBSim_PATH}/${OBJECT}")
endif()
endforeach(OBJECT)

foreach(_FILE ${SOURCE_FILES})
get_tail(${_FILE} FILE_EXTENSION ".")
if((NOT ${FILE_EXTENSION} STREQUAL "h") AND (NOT ${FILE_EXTENSION} STREQUAL "hxx"))
string(APPEND JSBSIM_SOURCE_FILES "'${_FILE}',")
endif()
endforeach(_FILE)

configure_file(setup-package.py.in "${CMAKE_CURRENT_BINARY_DIR}/package/setup.py")

# setup.py build_ext is called with --force because dependencies and time stamps
# are managed by CMake so we don't want setup.py to check them as well.
add_custom_command(OUTPUT ${JSBSIM_PYTHON_MODULE}
DEPENDS ${SETUP_PY} ${JSBSIM_CXX} $<TARGET_FILE:libJSBSim>
COMMAND ${PYTHON_EXECUTABLE} ${SETUP_PY} build_ext -b ${CMAKE_BINARY_DIR}/tests --force
$<IF:$<BOOL:${USING_MSBUILD}>,--config,> $<IF:$<BOOL:${USING_MSBUILD}>,$<CONFIG>,>
COMMAND ${COPY_COMMAND} ${JSBSIM_CXX} "package"
COMMENT "Building Python modules...")

add_custom_target(PythonJSBSim ALL DEPENDS ${JSBSIM_PYTHON_MODULE})
Expand Down
4 changes: 4 additions & 0 deletions python/MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
recursive-include ./ *.h*
include *.pyx
include *.pxd
include src/simgear/xml/xmltok_*.c
18 changes: 18 additions & 0 deletions python/setup-package.py.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
from setuptools import setup
from distutils.extension import Extension


# Build & installation process for the JSBSim Python module
setup(
name="${PROJECT_NAME}",
version="${PROJECT_VERSION}",
url="https://github.com/JSBSim-Team/jsbsim",
author="Jon S. Berndt",
license="LGPL 2.1",
ext_modules=[Extension('jsbsim', ['${JSBSIM_CXX}', ${JSBSIM_SOURCE_FILES}],
include_dirs=['src'],
extra_compile_args=[${CXX_FLAGS},
"-DJSBSIM_VERSION=\"${PROJECT_VERSION}\"",
"-DHAVE_EXPAT_CONFIG_H"],
language='c++')],
setup_requires=["setuptools>=18.0", "cython>=0.25"])
5 changes: 4 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,14 @@ add_library(libJSBSim ${HEADERS} ${SOURCES}
$<TARGET_OBJECTS:Magvar>
$<TARGET_OBJECTS:Misc>
$<TARGET_OBJECTS:IOStreams>
$<TARGET_OBJECTS:Structure>
$<TARGET_OBJECTS:Simgear>
)

set_target_properties (libJSBSim PROPERTIES
OUTPUT_NAME JSBSim${STATIC_LIBNAME_SUFFIX}
VERSION ${LIBRARY_VERSION})
VERSION ${LIBRARY_VERSION}
TARGET_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})

target_link_libraries(libJSBSim ${JSBSIM_LINK_LIBRARIES})

Expand Down
5 changes: 4 additions & 1 deletion src/initialization/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,8 @@ set(HEADERS FGInitialCondition.h
FGTrimAxis.h)

add_library(Init OBJECT ${HEADERS} ${SOURCES})
set_target_properties(Init PROPERTIES TARGET_DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR})

install(FILES ${HEADERS} DESTINATION include/JSBSim/initialization COMPONENT devel)
install(FILES ${HEADERS} DESTINATION include/JSBSim/initialization
COMPONENT devel)
5 changes: 4 additions & 1 deletion src/input_output/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,8 @@ set(HEADERS FGGroundCallback.h
FGUDPInputSocket.h)

add_library(InputOutput OBJECT ${HEADERS} ${SOURCES})
set_target_properties(InputOutput PROPERTIES TARGET_DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR})

install(FILES ${HEADERS} DESTINATION include/JSBSim/input_output COMPONENT devel)
install(FILES ${HEADERS} DESTINATION include/JSBSim/input_output
COMPONENT devel)
2 changes: 2 additions & 0 deletions src/math/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,7 @@ set(HEADERS FGColumnVector3.h
FGParameterValue.h)

add_library(Math OBJECT ${HEADERS} ${SOURCES})
set_target_properties(Math PROPERTIES TARGET_DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR})

install(FILES ${HEADERS} DESTINATION include/JSBSim/math COMPONENT devel)
2 changes: 2 additions & 0 deletions src/models/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,7 @@ set(HEADERS FGAerodynamics.h
FGFCSChannel.h)

add_library(Models OBJECT ${HEADERS} ${SOURCES})
set_target_properties(Models PROPERTIES TARGET_DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR})

install(FILES ${HEADERS} DESTINATION include/JSBSim/models COMPONENT devel)
5 changes: 4 additions & 1 deletion src/models/atmosphere/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ set(HEADERS FGMSIS.h
FGWinds.h)

add_library(Atmosphere OBJECT ${HEADERS} ${SOURCES})
set_target_properties(Atmosphere PROPERTIES TARGET_DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR})

install(FILES ${HEADERS} DESTINATION include/JSBSim/models/atmosphere COMPONENT devel)
install(FILES ${HEADERS} DESTINATION include/JSBSim/models/atmosphere
COMPONENT devel)
5 changes: 4 additions & 1 deletion src/models/flight_control/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,8 @@ set(HEADERS FGDeadBand.h
FGDistributor.h)

add_library(FlightControl OBJECT ${HEADERS} ${SOURCES})
set_target_properties(FlightControl PROPERTIES TARGET_DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR})

install(FILES ${HEADERS} DESTINATION include/JSBSim/models/flight_control COMPONENT devel)
install(FILES ${HEADERS} DESTINATION include/JSBSim/models/flight_control
COMPONENT devel)
5 changes: 4 additions & 1 deletion src/models/propulsion/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,8 @@ set(HEADERS FGElectric.h
FGRotor.h)

add_library(Propulsion OBJECT ${HEADERS} ${SOURCES})
set_target_properties(Propulsion PROPERTIES TARGET_DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR})

install(FILES ${HEADERS} DESTINATION include/JSBSim/models/propulsion COMPONENT devel)
install(FILES ${HEADERS} DESTINATION include/JSBSim/models/propulsion
COMPONENT devel)
8 changes: 6 additions & 2 deletions src/simgear/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ add_subdirectory(misc)
add_subdirectory(xml)
add_subdirectory(io/iostreams)

set(JSBSIM_SIMGEAR_HDR compiler.h)
set(HEADERS compiler.h)

install(FILES ${JSBSIM_SIMGEAR_HDR} DESTINATION include/JSBSim/simgear COMPONENT devel)
add_library(Simgear OBJECT ${HEADERS})
set_target_properties(Simgear PROPERTIES TARGET_DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR})

install(FILES ${HEADERS} DESTINATION include/JSBSim/simgear COMPONENT devel)
5 changes: 4 additions & 1 deletion src/simgear/io/iostreams/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ set(SOURCES sgstream.cxx)
set(HEADERS sgstream.hxx)

add_library(IOStreams OBJECT ${HEADERS} ${SOURCES})
set_target_properties(IOStreams PROPERTIES TARGET_DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR})

install(FILES ${HEADERS} DESTINATION include/JSBSim/simgear/io/iostreams COMPONENT devel)
install(FILES ${HEADERS} DESTINATION include/JSBSim/simgear/io/iostreams
COMPONENT devel)
5 changes: 4 additions & 1 deletion src/simgear/magvar/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ set(SOURCES coremag.cxx)
set(HEADERS coremag.hxx)

add_library(Magvar OBJECT ${HEADERS} ${SOURCES})
set_target_properties(Magvar PROPERTIES TARGET_DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR})

install(FILES ${HEADERS} DESTINATION include/JSBSim/simgear/magvar COMPONENT devel)
install(FILES ${HEADERS} DESTINATION include/JSBSim/simgear/magvar
COMPONENT devel)
5 changes: 4 additions & 1 deletion src/simgear/misc/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ set(SOURCES sg_path.cxx strutils.cxx)
set(HEADERS stdint.hxx sg_path.hxx strutils.hxx)

add_library(Misc OBJECT ${HEADERS} ${SOURCES})
set_target_properties(Misc PROPERTIES TARGET_DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR})

install(FILES ${HEADERS} DESTINATION include/JSBSim/simgear/misc COMPONENT devel)
install(FILES ${HEADERS} DESTINATION include/JSBSim/simgear/misc
COMPONENT devel)
5 changes: 4 additions & 1 deletion src/simgear/props/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@ set(SOURCES props.cxx propertyObject.cxx)
set(HEADERS props.hxx propertyObject.hxx)

add_library(Properties OBJECT ${HEADERS} ${SOURCES})
set_target_properties(Properties PROPERTIES TARGET_DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR})

install(FILES ${HEADERS} DESTINATION include/JSBSim/simgear/props COMPONENT devel)
install(FILES ${HEADERS} DESTINATION include/JSBSim/simgear/props
COMPONENT devel)
11 changes: 8 additions & 3 deletions src/simgear/structure/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
set(JSBSIM_SIMGEAR_STRUCTURE_HDR SGReferenced.hxx
SGSharedPtr.hxx)
set(HEADERS SGReferenced.hxx
SGSharedPtr.hxx)

install(FILES ${JSBSIM_SIMGEAR_STRUCTURE_HDR} DESTINATION include/JSBSim/simgear/structure COMPONENT devel)
add_library(Structure OBJECT ${HEADERS})
set_target_properties(Structure PROPERTIES TARGET_DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR})

install(FILES ${HEADERS} DESTINATION include/JSBSim/simgear/structure
COMPONENT devel)
2 changes: 2 additions & 0 deletions src/simgear/xml/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,7 @@ if(NOT EXPAT_FOUND)
endif()

add_library(Xml OBJECT ${HEADERS} ${SOURCES})
set_target_properties(Xml PROPERTIES TARGET_DIRECTORY
${CMAKE_CURRENT_SOURCE_DIR})

install(FILES ${HEADERS} DESTINATION include/JSBSim/simgear/xml COMPONENT devel)

0 comments on commit 7714ad0

Please sign in to comment.