Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev/python files #8

Merged
merged 8 commits into from
Jan 29, 2019
39 changes: 39 additions & 0 deletions cmake/__init__.py.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# -*- coding: utf-8 -*-
# generated from catkin/cmake/template/__init__.py.in
# keep symbol table as clean as possible by deleting all unnecessary symbols

from os import path as os_path
from sys import path as sys_path

from pkgutil import extend_path

__extended_path = "@PYTHON_SOURCE_PATH@".split(";")
for p in reversed(__extended_path):
sys_path.insert(0, p)
del p
del sys_path

__path__ = extend_path(__path__, __name__)
del extend_path

__execfiles = []
for p in __extended_path:
src_init_file = os_path.join(p, __name__ + '.py')
if os_path.isfile(src_init_file):
__execfiles.append(src_init_file)
else:
src_init_file = os_path.join(p, __name__, '__init__.py')
if os_path.isfile(src_init_file):
__execfiles.append(src_init_file)
del src_init_file
del p
del os_path
del __extended_path

for __execfile in __execfiles:
with open(__execfile, 'r') as __fh:
exec(__fh.read())
del __fh
del __execfile
del __execfiles

24 changes: 15 additions & 9 deletions cmake/customMacro.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
macro(SORT_SUBDIRS)
message("== Before sorting dependencies subdirs: ${SUBDIRS}")
foreach(dir ${SUBDIRS})
set(cmd "grep -r \"find_package(${dir})\" | awk -F \"/\" '{print $1}'")
set(cmd "grep -r \"find_package(${dir})\" | awk -F \"/\" '{print $1}'") #"
execute_process(COMMAND bash -c ${cmd}
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
RESULT_VARIABLE result
Expand Down Expand Up @@ -39,13 +39,16 @@ macro(SUBDIRLIST result curdir)
set(${result} ${dirlist})
endmacro()

macro(INIT_PKG)
string(TOUPPER ${PROJECT_NAME} ${PROJECT_NAME}_UPPER)
endmacro()
# build a debian package out of the cmake config with cpack
# a debian.cmake is necessary in your package
macro(BUILDDEB)
if(EXISTS "${CMAKE_ROOT}/Modules/CPack.cmake")
include(InstallRequiredSystemLibraries)
set(CPACK_SET_DESTDIR "on")
set(CPACK_PACKAGING_INSTALL_PREFIX "/tmp")
set(CPACK_SET_DESTDIR true)
set(CPACK_INSTALL_PREFIX "/opt/guillaume")
set(CPACK_GENERATOR "DEB")
set(CPACK_PACKAGE_DESCRIPTION "${DESCRIPTION}")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "${SUMMARY}")
Expand All @@ -54,6 +57,7 @@ macro(BUILDDEB)
set(CPACK_PACKAGE_VERSION_MAJOR "${MAJOR_VERSION}")
set(CPACK_PACKAGE_VERSION_MINOR "${MINOR_VERSION}")
set(CPACK_PACKAGE_VERSION_PATCH "${PATCH_VERSION}")
set(CPACK_INSTALL_CMAKE_PROJECTS "${CMAKE_CURRENT_BINARY_DIR};${PROJECT_NAME};${${PROJECT_NAME}_UPPER};/") # remove this line if you don't want to use the components packaging
set(CPACK_PACKAGE_FILE_NAME "${PROJECT_NAME}_${PKG_VERSION}")
set(CPACK_SOURCE_PACKAGE_FILE_NAME "${PROJECT_NAME}_${MAJOR_VERSION}.${MINOR_VERSION}.${CPACK_PACKAGE_VERSION_PATCH}")

Expand Down Expand Up @@ -91,6 +95,7 @@ macro(SETUP_PKG PKG_NAME)
export(TARGETS ${TARGETS_LIST} FILE "${PROJECT_BINARY_DIR}/${PKG_NAME}.cmake")
export(PACKAGE ${PKG_NAME})


include(${CMAKE_CURRENT_SOURCE_DIR}/debian.cmake)
set(PKG_VERSION ${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_VERSION})
set(${PKG_NAME}_VERSION "${PKG_VERSION}" PARENT_SCOPE)
Expand All @@ -109,8 +114,8 @@ macro(SETUP_PKG PKG_NAME)
install(FILES
${PROJECT_BINARY_DIR}/${PKG_NAME}Config.cmake
${PROJECT_BINARY_DIR}/${PKG_NAME}ConfigVersion.cmake
DESTINATION lib/${PROJECT_NAME})
install(EXPORT ${PKG_NAME} DESTINATION lib/${PROJECT_NAME})
DESTINATION lib/${PROJECT_NAME} COMPONENT ${${PROJECT_NAME}_UPPER})
install(EXPORT ${PKG_NAME} DESTINATION lib/${PROJECT_NAME} COMPONENT ${${PROJECT_NAME}_UPPER})

endmacro()

Expand All @@ -134,15 +139,16 @@ endmacro()
# Macro to install targets
macro(INSTALL_BIN_LIB PKG_NAME)
ARGN_TO_STRING(${ARGN})
string(TOUPPER ${PROJECT_NAME} ${PROJECT_NAME}_UPPER)
install (TARGETS ${LIST_STRING}
EXPORT ${PKG_NAME}
ARCHIVE DESTINATION lib/${PROJECT_NAME}
LIBRARY DESTINATION lib/${PROJECT_NAME}
RUNTIME DESTINATION bin/${PROJECT_NAME})
ARCHIVE DESTINATION lib/${PROJECT_NAME} COMPONENT ${${PROJECT_NAME}_UPPER}
LIBRARY DESTINATION lib/${PROJECT_NAME} COMPONENT ${${PROJECT_NAME}_UPPER}
RUNTIME DESTINATION bin/${PROJECT_NAME} COMPONENT ${${PROJECT_NAME}_UPPER})
endmacro()

# Macro to install headers
macro(INSTALL_ALL_HEADERS)
install(DIRECTORY include/ DESTINATION include)
install(DIRECTORY include/ DESTINATION include COMPONENT ${${PROJECT_NAME}_UPPER})
endmacro()

27 changes: 27 additions & 0 deletions cmake/python.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

macro(PREPARE_PYTHON)
set(CMAKE_PYTHON_BUILD "${ROOT_DIR}/build/python/lib/python3.5/site-packages")
set(CMAKE_PYTHON_INSTALL "${ROOT_DIR}/install/lib/python3/dist-packages")
file(MAKE_DIRECTORY ${CMAKE_PYTHON_BUILD})
file(MAKE_DIRECTORY ${CMAKE_PYTHON_INSTALL})
endmacro()


macro(SETUP_PYTHON_PKG)
set(ENV{PYTHONPATH} "$ENV{PYTHONPATH}:${CMAKE_PYTHON_BUILD}")
file(MAKE_DIRECTORY ${CMAKE_PYTHON_BUILD}/${PROJECT_NAME})
set(INIT_PKG_BUILD ${CMAKE_PYTHON_BUILD}/${PROJECT_NAME}/__init__.py)
set(PYTHON_SOURCE_PATH "${PROJECT_SOURCE_DIR}/src")
configure_file(${ROOT_DIR}/cmake/__init__.py.in ${INIT_PKG_BUILD})

set(PATH_TO_${PROJECT_NAME}_PY_FILES "${PROJECT_SOURCE_DIR}/src/${PROJECT_NAME}")
install(DIRECTORY ${PATH_TO_${PROJECT_NAME}_PY_FILES} DESTINATION lib/python3/dist-packages COMPONENT ${${PROJECT_NAME}_UPPER})

endmacro()

macro(INSTALL_PYTHON_SCRIPTS)
set (SCRIPTS ${ARGN})
install(PROGRAMS ${SCRIPTS} DESTINATION lib/${PROJECT_NAME} COMPONENT ${${PROJECT_NAME}_UPPER}
)
endmacro()

1 change: 1 addition & 0 deletions setup.zsh
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
MY_SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
CMAKE_PREFIX_PATH=${MY_SCRIPTPATH}/build:${CMAKE_PREFIX_PATH}
PYTHONPATH=/home/beuzeboc/code/tmp/cmake_multi_repo_template/build/python/lib/python3.5/site-packages:${PYTHONPATH}
1 change: 1 addition & 0 deletions setup_install.zsh
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
MY_SCRIPTPATH="$( cd "$(dirname "$0")" ; pwd -P )"
CMAKE_PREFIX_PATH=${MY_SCRIPTPATH}/install:${CMAKE_PREFIX_PATH}
PYTHONPATH=/home/beuzeboc/code/tmp/cmake_multi_repo_template/install/lib/python3/dist-packages:${PYTHONPATH}
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ cmake_minimum_required(VERSION 3.5)
project(workspace)

include(../cmake/customMacro.cmake)
include(../cmake/python.cmake)
set(ROOT_DIR "${CMAKE_SOURCE_DIR}/..")
set(CMAKE_INSTALL_PREFIX "${ROOT_DIR}/install")
file(MAKE_DIRECTORY ${CMAKE_INSTALL_PREFIX})

PREPARE_PYTHON()

SUBDIRLIST(SUBDIRS ${CMAKE_CURRENT_SOURCE_DIR})
SORT_SUBDIRS()
FOREACH(subdir ${SUBDIRS})
Expand Down
7 changes: 7 additions & 0 deletions src/a_lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
project(a_lib VERSION 0.1 LANGUAGES CXX)

find_package( Boost COMPONENTS date_time REQUIRED )

INIT_PKG()

SETUP_PYTHON_PKG()

include_directories(include ${Boost_INCLUDE_DIR} )
add_library(a_lib
src/lib.cpp)
Expand All @@ -17,6 +22,8 @@ INSTALL_BIN_LIB(a_lib a_lib)

INSTALL_ALL_HEADERS()

INSTALL_PYTHON_SCRIPTS(scripts/a_test.py)

SETUP_PKG(a_lib a_lib) # First arg is pkg name and after they are the targets names

BUILDDEB()
6 changes: 6 additions & 0 deletions src/a_lib/scripts/a_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/python3

from a_lib.a_lib import a_lib_function

if __name__ == '__main__':
a_lib_function()
Empty file added src/a_lib/src/a_lib/__init__.py
Empty file.
4 changes: 4 additions & 0 deletions src/a_lib/src/a_lib/a_lib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/python3

def a_lib_function():
print("Hello from a_lib new")
7 changes: 7 additions & 0 deletions src/g_app/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
project(g_app)
find_package(a_lib)
find_package(z_lib)

INIT_PKG()

SETUP_PYTHON_PKG()

add_executable(g_app
src/app.cpp)

Expand All @@ -9,6 +14,8 @@ target_link_libraries(g_app z_lib a_lib)

INSTALL_BIN_LIB(g_app g_app)

INSTALL_PYTHON_SCRIPTS(scripts/test.py)

SETUP_PKG(g_app g_app) # First arg is pkg name and after they are the targets names

BUILDDEB()
Expand Down
6 changes: 6 additions & 0 deletions src/g_app/scripts/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/usr/bin/python3

from g_app.app import main

if __name__ == '__main__':
main()
Empty file added src/g_app/src/g_app/__init__.py
Empty file.
10 changes: 10 additions & 0 deletions src/g_app/src/g_app/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/python3

from a_lib import a_lib
from z_lib import z_lib

def main():
a_lib.a_lib_function()
z_lib.z_lib_function()
print("HelloWorld")

5 changes: 5 additions & 0 deletions src/z_lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ project(z_lib)

find_package( Boost COMPONENTS date_time REQUIRED )
find_package(Eigen3 REQUIRED)

INIT_PKG()

SETUP_PYTHON_PKG()

include_directories(include ${Boost_INCLUDE_DIR} ${EIGEN3_INCLUDE_DIR})
add_library(z_lib
src/lib.cpp)
Expand Down
Empty file added src/z_lib/src/z_lib/__init__.py
Empty file.
4 changes: 4 additions & 0 deletions src/z_lib/src/z_lib/z_lib.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/python3

def z_lib_function():
print("hello from z lib")