Skip to content

Commit

Permalink
Merge pull request #947 from stlink-org/pkg
Browse files Browse the repository at this point in the history
[refactoring] Cleanup of cmake build process
  • Loading branch information
Nightwalker-87 authored Apr 26, 2020
2 parents b53a207 + f827006 commit 5899409
Show file tree
Hide file tree
Showing 47 changed files with 422 additions and 416 deletions.
155 changes: 89 additions & 66 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ cmake_policy(SET CMP0042 NEW)
set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};${CMAKE_SOURCE_DIR}/cmake/modules")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)


###
Expand All @@ -25,10 +26,10 @@ option(STLINK_INSTALL_UDEV_RULES "Install udev rules files" ON)
option(STLINK_INSTALL_MODPROBE_CONF "Install modprobe conf files" ON)
option(STLINK_GENERATE_MANPAGES "Generate manpages with pandoc" OFF)

# Determine project version
## Determine project version
include(${CMAKE_MODULE_PATH}/get_version.cmake)

# Set C build flags
## Set C build flags
if (NOT MSVC)
include(${CMAKE_MODULE_PATH}/c_flags.cmake)
else ()
Expand All @@ -39,11 +40,7 @@ else ()
set(CMAKE_C_FLAGS_RELWITHDEBINFO_INIT "/MT /Zi /O2 /Ob1 /D NDEBUG")
endif ()


# ====

#set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)

## Set installation directories for libraries
if (IS_DIRECTORY ${LIB_INSTALL_DIR})
set(LIB_INSTALL_DIR ${LIB_INSTALL_DIR} CACHE PATH "Main library directory")
set(STLINK_LIBRARY_PATH "${LIB_INSTALL_DIR}")
Expand All @@ -52,6 +49,7 @@ else ()
set(STLINK_LIBRARY_PATH "${CMAKE_INSTALL_PREFIX}/${LIB_INSTALL_DIR}")
endif ()

## Set installation directories for header files
if (IS_DIRECTORY ${INCLUDE_INSTALL_DIR})
set(INCLUDE_INSTALL_DIR ${INCLUDE_INSTALL_DIR} CACHE PATH "Main include directory")
set(STLINK_INCLUDE_PATH "${INCLUDE_INSTALL_DIR}")
Expand All @@ -67,11 +65,14 @@ endif ()

find_package(libusb REQUIRED)

## Package configuration (pkg-config) on unix-based systems
if (NOT WIN32 AND NOT CMAKE_CROSSCOMPILING)
add_subdirectory(cmake/pkgconfig)
find_package(PkgConfig)
pkg_check_modules(GTK3 gtk+-3.0)
endif ()

## Check for system-specific additional header files and libraries
include(CheckIncludeFile)

CHECK_INCLUDE_FILE(sys/mman.h STLINK_HAVE_SYS_MMAN_H)
Expand All @@ -93,51 +94,64 @@ else ()
set(SSP_LIB "")
endif ()

if (CMAKE_BUILD_TYPE STREQUAL "")
set(CMAKE_BUILD_TYPE "Debug")
endif ()

if (${CMAKE_BUILD_TYPE} MATCHES "Debug")
include(CTest)
endif ()
###
# Main build process
###

## Define include directories to avoid absolute paths for header defines
include_directories(${LIBUSB_INCLUDE_DIR})

# ====
include_directories(include) ### TODO: Clean this up...
include_directories(${PROJECT_BINARY_DIR}/include/stlink)
include_directories(include/stlink)
include_directories(include/stlink/tools)
# ====

include_directories(src)

set(STLINK_HEADERS
include/stlink.h
include/stlink/usb.h
include/stlink/sg.h
include/stlink/logging.h
include/stlink/mmap.h
include/stlink/chipid.h
include/stlink/flash_loader.h
)
include/stlink.h
include/stlink/backend.h
include/stlink/chipid.h
include/stlink/commands.h
include/stlink/flash_loader.h
include/stlink/reg.h
src/logging.h
src/md5.h
src/sg.h
src/usb.h
)

set(STLINK_SOURCE
src/chipid.c
src/common.c
src/usb.c
src/sg.c
src/logging.c
src/flash_loader.c
src/md5.c
)
src/common.c
src/chipid.c
src/flash_loader.c
src/logging.c
src/md5.c
src/sg.c
src/usb.c
)

if (WIN32 OR MINGW OR MSYS)
include_directories(src/mingw)
set(STLINK_SOURCE "${STLINK_SOURCE};src/mmap.c;src/mingw/mingw.c")
set(STLINK_HEADERS "${STLINK_HEADERS};src/mingw/mingw.h")
set(STLINK_HEADERS "${STLINK_HEADERS};src/mmap.h;src/mingw/mingw.h")
endif ()

include_directories(${LIBUSB_INCLUDE_DIR})
include_directories(include)
include_directories(${PROJECT_BINARY_DIR}/include)
include_directories(src/mingw)

if (MSVC)
include_directories(src/win32)
include_directories(src/getopt)
# Use string.h rather than strings.h and disable annoying warnings
add_definitions(-DHAVE_STRING_H -D_CRT_SECURE_NO_WARNINGS -D_CRT_NONSTDC_NO_WARNINGS /wd4710)
endif ()

## Include test execution for test-targets for target Debug
if (${CMAKE_BUILD_TYPE} MATCHES "Debug")
include(CTest)
endif ()


###
# Shared library
Expand All @@ -150,22 +164,24 @@ else (WIN32)
endif ()

add_library(
${STLINK_LIB_SHARED} SHARED
${STLINK_HEADERS} # header files for ide projects generated by cmake
${STLINK_SOURCE}
)
${STLINK_LIB_SHARED} SHARED
${STLINK_HEADERS} # header files for ide projects generated by cmake
${STLINK_SOURCE}
)

target_link_libraries(
${STLINK_LIB_SHARED}
${LIBUSB_LIBRARY}
)
${STLINK_LIB_SHARED}
${LIBUSB_LIBRARY}
)

set(STLINK_SHARED_VERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH})

message(STATUS "STLINK_LIB_SHARED: ${STLINK_LIB_SHARED}")
message(STATUS "PROJECT_VERSION_MAJOR: ${PROJECT_VERSION_MAJOR}")
message(STATUS "VERSION: ${STLINK_SHARED_VERSION}")

set_target_properties(${STLINK_LIB_SHARED} PROPERTIES
set_target_properties(
${STLINK_LIB_SHARED} PROPERTIES
SOVERSION ${PROJECT_VERSION_MAJOR}
VERSION ${STLINK_SHARED_VERSION}
)
Expand All @@ -184,7 +200,7 @@ else ()
target_link_libraries(${STLINK_LIB_SHARED} ${LIBUSB_LIBRARY} ${SSP_LIB})
endif ()

install(TARGETS ${STLINK_LIB_SHARED} DESTINATION ${STLINK_LIBRARY_PATH}) ### TODO: Check path
install(TARGETS ${STLINK_LIB_SHARED} DESTINATION ${STLINK_LIBRARY_PATH})


###
Expand All @@ -199,6 +215,11 @@ add_library(
${STLINK_SOURCE}
)

set_target_properties(
${STLINK_LIB_STATIC} PROPERTIES
OUTPUT_NAME ${PROJECT_NAME}
)

# Link static library with Apple macOS libraries
if (APPLE)
find_library(ObjC objc)
Expand All @@ -207,36 +228,42 @@ if (APPLE)
target_link_libraries(${STLINK_LIB_STATIC} ${CoreFoundation} ${IOKit} ${ObjC} ${SSP_LIB})
endif ()

if (WIN32 OR MINGW OR MSYS) ### TODO: MinGW OR MSYS on Linux
if (WIN32 OR MINGW OR MSYS)
target_link_libraries(${STLINK_LIB_STATIC} ${LIBUSB_LIBRARY} wsock32 ws2_32 ${SSP_LIB})
else ()
target_link_libraries(${STLINK_LIB_STATIC} ${LIBUSB_LIBRARY} ${SSP_LIB})
endif ()

set_target_properties(${STLINK_LIB_STATIC} PROPERTIES OUTPUT_NAME ${PROJECT_NAME})

if (STLINK_STATIC_LIB)
install(TARGETS ${STLINK_LIB_STATIC} ARCHIVE DESTINATION ${STLINK_LIBRARY_PATH}) ### TODO: Check path
install(TARGETS ${STLINK_LIB_STATIC} ARCHIVE DESTINATION ${STLINK_LIBRARY_PATH})
endif ()


###
# Tools
# Build toolset executables
###

set(ST-UTIL_SOURCES src/st-util/gdb-remote.c src/st-util/gdb-server.c src/st-util/semihosting.c)

if (MSVC)
# Add getopt to sources
set(ST-UTIL_SOURCES "${ST-UTIL_SOURCES};src/getopt/getopt.c")
endif ()

add_executable(st-flash src/tools/flash.c src/tools/flash_opts.c)
add_executable(st-info src/tools/info.c)
add_executable(st-util ${ST-UTIL_SOURCES})

if (WIN32 OR APPLE)
target_link_libraries(st-flash ${STLINK_LIB_STATIC} ${SSP_LIB})
target_link_libraries(st-info ${STLINK_LIB_STATIC} ${SSP_LIB})
target_link_libraries(st-util ${STLINK_LIB_STATIC} ${SSP_LIB})
else ()
target_link_libraries(st-flash ${STLINK_LIB_SHARED} ${SSP_LIB})
target_link_libraries(st-info ${STLINK_LIB_SHARED} ${SSP_LIB})
target_link_libraries(st-util ${STLINK_LIB_SHARED} ${SSP_LIB})
endif ()

install(TARGETS st-flash st-info RUNTIME DESTINATION bin) ### TODO: Check path

if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
if (STLINK_INSTALL_MODPROBE_CONF)
install(FILES etc/modprobe.d/stlink_v1.conf DESTINATION ${STLINK_MODPROBED_DIR}/)
Expand All @@ -247,27 +274,23 @@ if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
endif ()
endif ()

add_subdirectory(src/gdbserver)
add_subdirectory(src/stlink-gui)

install(TARGETS st-flash DESTINATION bin)
install(TARGETS st-info DESTINATION bin)
install(TARGETS st-util DESTINATION bin)

###
# Others
# Additional build tasks
###

add_subdirectory(include) ### TODO: Check path
add_subdirectory(doc/man)
add_subdirectory(tests)

# ====
add_subdirectory(include) # contains subordinate CMakeLists for version config and old header includes
### TODO: Clean this up ...
# ====


###
# Package build
###

add_subdirectory(cmake/packaging)
include(cmake/packaging/cpack_config.cmake)
add_subdirectory(src/stlink-gui) # contains subordinate CMakeLists to build GUI
add_subdirectory(tests) # contains subordinate CMakeLists to build test executables
add_subdirectory(doc/man) # contains subordinate CMakeLists to generate manpages
add_subdirectory(cmake/packaging) # contains subordinate CMakeLists to build packages


###
Expand Down
2 changes: 1 addition & 1 deletion LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BSD 3-Clause License

Copyright (c) 2020, The stlink project (github.com/stlink-org/stlink) & "Capt'ns Missing Link" authors.
Copyright (c) 2020, stlink-org
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand Down
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ help:
@echo " debug: Run a debug build"
@echo " release: Run a release build"
@echo " install: Install release build"
@echo " uninstall: Uninstall release build"
@echo " package: Package release build"
@echo " lint: Lint check all source-code"
@echo " test: Build and run tests"
Expand All @@ -35,11 +36,16 @@ install: build/Release
@echo "[INSTALL] Release"
@$(MAKE) -C build/Release install

uninstall: build/Release
@echo "[UNINSTALL] Release"
@$(MAKE) -C build/Release uninstall

package: build/Release
@echo "[PACKAGE] Release"
@$(MAKE) -C build/Release package

test: debug
@echo "[TEST]"
@$(MAKE) -C build/Debug test

build/Debug:
Expand Down
7 changes: 0 additions & 7 deletions cmake/modules/Find7zip.cmake

This file was deleted.

17 changes: 5 additions & 12 deletions cmake/modules/Findlibusb.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ elseif (WIN32) # Windows

if (NOT LIBUSB_FOUND OR EXISTS "/etc/debian_version")
# Preparations for installing libusb library
find_package(7zip REQUIRED)
set(LIBUSB_WIN_VERSION 1.0.23) # set libusb version
set(LIBUSB_WIN_ARCHIVE libusb-${LIBUSB_WIN_VERSION}.7z)
set(LIBUSB_WIN_ARCHIVE_PATH ${CMAKE_BINARY_DIR}/${LIBUSB_WIN_ARCHIVE})
Expand All @@ -92,17 +91,11 @@ elseif (WIN32) # Windows

file(MAKE_DIRECTORY ${LIBUSB_WIN_OUTPUT_FOLDER})

# Extract libusb package
if (${ZIP_EXECUTABLE} MATCHES "p7zip")
execute_process(
COMMAND ${ZIP_EXECUTABLE} -d ${LIBUSB_WIN_ARCHIVE_PATH}
WORKING_DIRECTORY ${LIBUSB_WIN_OUTPUT_FOLDER}
)
else ()
execute_process(
COMMAND ${ZIP_EXECUTABLE} x -y ${LIBUSB_WIN_ARCHIVE_PATH} -o${LIBUSB_WIN_OUTPUT_FOLDER}
) # <-- Note the absence of a space character following the -o option!
endif ()
# Extract libusb package with cmake
execute_process(
COMMAND ${CMAKE_COMMAND} -E tar xv ${LIBUSB_WIN_ARCHIVE_PATH}
WORKING_DIRECTORY ${LIBUSB_WIN_OUTPUT_FOLDER}
)

# Find path to libusb library
FIND_PATH(
Expand Down
2 changes: 2 additions & 0 deletions cmake/packaging/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ add_subdirectory(debian)
add_subdirectory(fedora)
add_subdirectory(opensuse)
add_subdirectory(windows)

include(cpack_config.cmake)
Loading

0 comments on commit 5899409

Please sign in to comment.