Skip to content

Commit

Permalink
Updated cmake configuration
Browse files Browse the repository at this point in the history
- Alligned formatting
- Updated version requirement
- Updated pkg-maintainer
- [doc] Added note in compiling manual
- Fixed install paths in build script
- Updated C-flag -std=gnu99 to gnu11
  • Loading branch information
Nightwalker-87 committed Apr 5, 2020
1 parent 27aa888 commit 587fb48
Show file tree
Hide file tree
Showing 21 changed files with 259 additions and 267 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
build
build-mingw
obj-*
*.user*
173 changes: 85 additions & 88 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,78 +1,81 @@
cmake_minimum_required(VERSION 2.8.7)
cmake_minimum_required(VERSION 3.4.2)
set(CMAKE_USER_MAKE_RULES_OVERRIDE cmake/c_flag_overrides.cmake)

project(stlink C)
set(PROJECT_DESCRIPTION "Open source version of the STMicroelectronics Stlink Tools")
set(PROJECT_DESCRIPTION "Open source version of the STMicroelectronics ST-LINK Tools")
set(STLINK_UDEV_RULES_DIR "/etc/udev/rules.d" CACHE PATH "Udev rules directory")
set(STLINK_MODPROBED_DIR "/etc/modprobe.d" CACHE PATH "modprobe.d directory")
set(STLINK_STATIC_LIB ON CACHE BOOL "Install static lib")

if( IS_DIRECTORY ${LIB_INSTALL_DIR})
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)

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}")
else()
else ()
set(LIB_INSTALL_DIR "lib" CACHE PATH "Main library directory")
set(STLINK_LIBRARY_PATH "${CMAKE_INSTALL_PREFIX}/${LIB_INSTALL_DIR}" )
endif()
endif ()

if( IS_DIRECTORY ${INCLUDE_INSTALL_DIR})
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}" )
else()
else ()
set(INCLUDE_INSTALL_DIR "include" CACHE PATH "Main include directory")
set(STLINK_INCLUDE_PATH "${CMAKE_INSTALL_PREFIX}/${INCLUDE_INSTALL_DIR}")
endif()
endif ()

option(STLINK_GENERATE_MANPAGES "Generate manpages with pandoc" OFF)
option(STLINK_INSTALL_UDEV_RULES "Install udev rules files" ON)
option(STLINK_INSTALL_MODPROBE_CONF "Install modprobe conf files" ON)

if (POLICY CMP0042)
# Newer cmake on MacOS should use @rpath
cmake_policy (SET CMP0042 NEW)
endif ()
# cmake (3.0+) on MacOS should use @rpath
cmake_policy(SET CMP0042 NEW)

set(CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};${CMAKE_SOURCE_DIR}/cmake/modules")
include(cmake/Version.cmake)
if(NOT MSVC)
include(cmake/CFlags.cmake)
endif()
include(cmake/version.cmake)
if (NOT MSVC)
include(cmake/c_flags.cmake)
endif ()

###
# Dependencies
###
find_package(LibUSB REQUIRED)
if (NOT APPLE AND NOT WIN32 AND NOT CMAKE_CROSSCOMPILING)
find_package(PkgConfig)
pkg_check_modules(gtk gtk+-3.0)
find_package(PkgConfig)
pkg_check_modules(gtk gtk+-3.0)
endif ()

include(CheckIncludeFile)
CHECK_INCLUDE_FILE(sys/mman.h STLINK_HAVE_SYS_MMAN_H)
if (STLINK_HAVE_SYS_MMAN_H)
add_definitions(-DSTLINK_HAVE_SYS_MMAN_H)
endif()
add_definitions(-DSTLINK_HAVE_SYS_MMAN_H)
endif ()

CHECK_INCLUDE_FILE(unistd.h STLINK_HAVE_UNISTD_H)
if (STLINK_HAVE_UNISTD_H)
add_definitions(-DSTLINK_HAVE_UNISTD_H)
endif()
add_definitions(-DSTLINK_HAVE_UNISTD_H)
endif ()

include(CheckLibraryExists)
CHECK_LIBRARY_EXISTS(ssp __stack_chk_fail "" _stack_chk_fail_exists)

if(_stack_chk_fail_exists)
if (_stack_chk_fail_exists)
set(SSP_LIB ssp)
else()
else ()
set(SSP_LIB "")
endif()
endif ()

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

if (${CMAKE_BUILD_TYPE} MATCHES "Debug")
include(CTest)
endif()
include(CTest)
endif ()

set(STLINK_HEADERS
include/stlink.h
Expand All @@ -82,7 +85,6 @@ set(STLINK_HEADERS
include/stlink/mmap.h
include/stlink/chipid.h
include/stlink/flash_loader.h
include/stlink/stlinkusb.h
)

set(STLINK_SOURCE
Expand All @@ -95,64 +97,65 @@ set(STLINK_SOURCE
)

if (WIN32 OR MSYS OR MINGW)
set (STLINK_SOURCE "${STLINK_SOURCE};src/mmap.c;src/mingw/mingw.c")
set (STLINK_HEADERS "${STLINK_HEADERS};src/mingw/mingw.h")
set (STLINK_SOURCE "${STLINK_SOURCE};src/mmap.c;src/mingw/mingw.c")
set (STLINK_HEADERS "${STLINK_HEADERS};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)
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 ()

###
# Shared library
###
if (NOT WIN32)
set(STLINK_LIB_SHARED ${PROJECT_NAME})
set(STLINK_LIB_SHARED ${PROJECT_NAME})
else ()
set(STLINK_LIB_SHARED ${PROJECT_NAME}-shared)
endif()
set(STLINK_LIB_SHARED ${PROJECT_NAME}-shared)
endif ()

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

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

if (WIN32 OR MSYS OR MINGW)
set(STLINK_SHARED_VERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR})
else()
set(STLINK_SHARED_VERSION ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}.${PROJECT_VERSION_PATCH})
endif()

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 SOVERSION ${PROJECT_VERSION_MAJOR}
VERSION ${STLINK_SHARED_VERSION}
)
PROPERTIES SOVERSION ${PROJECT_VERSION_MAJOR}
VERSION ${STLINK_SHARED_VERSION}
)

# Link shared library with apple OS libraries
if (APPLE)
find_library(ObjC objc)
find_library(CoreFoundation CoreFoundation)
find_library(IOKit IOKit)
find_library(ObjC objc)
find_library(CoreFoundation CoreFoundation)
find_library(IOKit IOKit)
target_link_libraries(${STLINK_LIB_SHARED} ${CoreFoundation} ${IOKit} ${ObjC} ${SSP_LIB})
endif()
endif ()

if (WIN32 OR MSYS OR MINGW)
target_link_libraries(${STLINK_LIB_SHARED} ${LIBUSB_LIBRARY} wsock32 ws2_32 ${SSP_LIB})
else()
else ()
target_link_libraries(${STLINK_LIB_SHARED} ${LIBUSB_LIBRARY} ${SSP_LIB})
endif()
endif ()


install(TARGETS ${STLINK_LIB_SHARED}
Expand All @@ -165,64 +168,58 @@ install(TARGETS ${STLINK_LIB_SHARED}
set(STLINK_LIB_STATIC ${PROJECT_NAME}-static)

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

# Link shared library with apple OS libraries
if (APPLE)
find_library(ObjC objc)
find_library(CoreFoundation CoreFoundation)
find_library(IOKit IOKit)
find_library(ObjC objc)
find_library(CoreFoundation CoreFoundation)
find_library(IOKit IOKit)
target_link_libraries(${STLINK_LIB_STATIC} ${CoreFoundation} ${IOKit} ${ObjC} ${SSP_LIB})
endif()

if (WIN32 OR MSYS OR MINGW)
target_link_libraries(${STLINK_LIB_STATIC} ${LIBUSB_LIBRARY} wsock32 ws2_32 ${SSP_LIB})
else()
else ()
target_link_libraries(${STLINK_LIB_STATIC} ${LIBUSB_LIBRARY} ${SSP_LIB})
endif()
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}
)
endif()
install(TARGETS ${STLINK_LIB_STATIC} ARCHIVE DESTINATION ${STLINK_LIBRARY_PATH})
endif ()

###
# Tools
###
add_executable(st-flash src/tools/flash.c src/tools/flash_opts.c)
if (WIN32 OR APPLE)
target_link_libraries(st-flash ${STLINK_LIB_STATIC} ${SSP_LIB})
else()
else ()
target_link_libraries(st-flash ${STLINK_LIB_SHARED} ${SSP_LIB})
endif()
endif ()

add_executable(st-info src/tools/info.c)
if (WIN32 OR APPLE)
target_link_libraries(st-info ${STLINK_LIB_STATIC} ${SSP_LIB})
else()
else ()
target_link_libraries(st-info ${STLINK_LIB_SHARED} ${SSP_LIB})
endif()
endif ()

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

if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
if (STLINK_INSTALL_MODPROBE_CONF)
install(FILES etc/modprobe.d/stlink_v1.conf
DESTINATION ${STLINK_MODPROBED_DIR}/)
endif()
if (STLINK_INSTALL_UDEV_RULES)
file(GLOB RULES_FILES etc/udev/rules.d/*.rules)
install(FILES ${RULES_FILES}
DESTINATION ${STLINK_UDEV_RULES_DIR}/)
endif()
endif()
if (STLINK_INSTALL_MODPROBE_CONF)
install(FILES etc/modprobe.d/stlink_v1.conf DESTINATION ${STLINK_MODPROBED_DIR}/)
endif ()
if (STLINK_INSTALL_UDEV_RULES)
file(GLOB RULES_FILES etc/udev/rules.d/*.rules)
install(FILES ${RULES_FILES} DESTINATION ${STLINK_UDEV_RULES_DIR}/)
endif ()
endif ()

add_subdirectory(src/gdbserver)
add_subdirectory(src/tools/gui)
Expand All @@ -235,5 +232,5 @@ add_subdirectory(include)
add_subdirectory(doc/man)
add_subdirectory(tests)

include(cmake/CPackConfig.cmake)
include(cmake/cpack_config.cmake)
include(CPack)
27 changes: 0 additions & 27 deletions cmake/CPackConfig.cmake

This file was deleted.

2 changes: 1 addition & 1 deletion cmake/c_flag_overrides.cmake
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
if(MSVC)
message(STATUS "MSVC C Flags override to /MT")
set(CMAKE_C_FLAGS_DEBUG_INIT "/D_DEBUG /MTd /Zi /Ob0 /Od /RTC1")
set(CMAKE_C_FLAGS_DEBUG_INIT "/D_DEBUG /MTd /Zi /Ob0 /Od /RTC1")
set(CMAKE_C_FLAGS_MINSIZEREL_INIT "/MT /O1 /Ob1 /D NDEBUG")
set(CMAKE_C_FLAGS_RELEASE_INIT "/MT /O2 /Ob2 /D NDEBUG")
set(CMAKE_C_FLAGS_RELWITHDEBINFO_INIT "/MT /Zi /O2 /Ob1 /D NDEBUG")
Expand Down
12 changes: 6 additions & 6 deletions cmake/CFlags.cmake → cmake/c_flags.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function(add_cflag_if_supported flag)
endif()
endfunction()

add_cflag_if_supported("-std=gnu99")
add_cflag_if_supported("-std=gnu11")
add_cflag_if_supported("-Wall")
add_cflag_if_supported("-Wextra")
add_cflag_if_supported("-Wshadow")
Expand All @@ -34,17 +34,17 @@ add_cflag_if_supported("-Wimplicit-function-declaration")
# /usr/include/sys/types.h:218: warning: previous declaration of 'truncate' was here
##
if (NOT CMAKE_SYSTEM_NAME STREQUAL "OpenBSD")
add_cflag_if_supported("-Wredundant-decls")
add_cflag_if_supported("-Wredundant-decls")
endif ()

if (NOT WIN32)
add_cflag_if_supported("-fPIC")
add_cflag_if_supported("-fPIC")
endif ()

if(${CMAKE_BUILD_TYPE} MATCHES "Debug")
add_cflag_if_supported("-ggdb")
add_cflag_if_supported("-O0")
add_cflag_if_supported("-ggdb")
add_cflag_if_supported("-O0")
else()
add_cflag_if_supported("-O2")
add_cflag_if_supported("-O2")
add_cflag_if_supported("-Werror")
endif()
Loading

0 comments on commit 587fb48

Please sign in to comment.