From a35cc23525f985b6bd494a315189c326b33d29cd Mon Sep 17 00:00:00 2001 From: Andrew Andrianov Date: Sat, 9 Jul 2016 23:07:50 +0300 Subject: [PATCH] CMakeLists.txt: Add shared library build and debian packaging Signed-off-by: Andrew Andrianov --- CMakeLists.txt | 84 +++++++++++++++++++++++++++++++--- debian/changelog | 5 ++ debian/compat | 1 + debian/control | 34 ++++++++++++++ debian/libstlink-dev.dirs | 2 + debian/libstlink-dev.install | 3 ++ debian/libstlink.dirs | 1 + debian/libstlink.install | 1 + debian/rules | 28 ++++++++++++ debian/source/format | 1 + debian/stlink-gui.dirs | 2 + debian/stlink-gui.install | 2 + debian/stlink-tools.dirs | 2 + debian/stlink-tools.install | 1 + doc/app-example/CMakeLists.txt | 28 ++++++++++++ doc/app-example/README.md | 2 + doc/app-example/main.c | 29 ++++++++++++ pkg-config.pc.cmake | 11 +++++ scripts/run_clang_analyze.sh | 8 ++++ 19 files changed, 239 insertions(+), 6 deletions(-) create mode 100644 debian/changelog create mode 100644 debian/compat create mode 100644 debian/control create mode 100644 debian/libstlink-dev.dirs create mode 100644 debian/libstlink-dev.install create mode 100644 debian/libstlink.dirs create mode 100644 debian/libstlink.install create mode 100755 debian/rules create mode 100644 debian/source/format create mode 100644 debian/stlink-gui.dirs create mode 100644 debian/stlink-gui.install create mode 100644 debian/stlink-tools.dirs create mode 100644 debian/stlink-tools.install create mode 100644 doc/app-example/CMakeLists.txt create mode 100644 doc/app-example/README.md create mode 100644 doc/app-example/main.c create mode 100644 pkg-config.pc.cmake create mode 100755 scripts/run_clang_analyze.sh diff --git a/CMakeLists.txt b/CMakeLists.txt index fc0127d..27aa9cd 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,6 @@ cmake_minimum_required(VERSION 2.8.7) +project(stlink C) +set(PROJECT_DESCRIPTION "Open source version of the STMicroelectronics Stlink Tools") project(stlink C) include(CheckCCompilerFlag) @@ -18,7 +20,6 @@ if (DEFINED ENV{TRAVIS_TAG} AND NOT "$ENV{TRAVIS_TAG}" STREQUAL "") set (STLINK_PACKAGE_VERSION "$ENV{TRAVIS_TAG}") elseif (GIT_FOUND AND EXISTS "${PROJECT_SOURCE_DIR}/.git") # Working off a git repo, using git versioning - # Check if HEAD is pointing to a tag execute_process ( COMMAND "${GIT_EXECUTABLE}" describe --always @@ -40,9 +41,23 @@ elseif (EXISTS ${PROJECT_SOURCE_DIR}/.version) # we can extract the package version from .version file. file (STRINGS .version STLINK_PACKAGE_VERSION) else () - set (STLINK_PACKAGE_VERSION "Unknown") + set (STLINK_PACKAGE_VERSION "999.99.99-unknown") endif() +# We shall use STLINK_PACKAGE_VERSION* variables to set soversion and other stuff +# Since these numbers will appear in .so names, e.g. libstlink.so.1.2.0 +# We can't just use STLINK_PACKAGE_VERSION here +# (Well, we can, but that breaks all the existing conventions) +# We can't as well use PROJECT_VERSION* variables here, since they are managed +# by project() directive and the whole version detection relies on PROJECT_SOURCE_DIR + +string(REPLACE "." ";" STLINK_PACKAGE_VERSION_ARRAY ${STLINK_PACKAGE_VERSION}) +string(REPLACE "-" ";" STLINK_PACKAGE_VERSION_ARRAY "${STLINK_PACKAGE_VERSION_ARRAY}") +list(GET STLINK_PACKAGE_VERSION_ARRAY 0 STLINK_PACKAGE_VERSION_MAJOR) +list(GET STLINK_PACKAGE_VERSION_ARRAY 1 STLINK_PACKAGE_VERSION_MINOR) +list(GET STLINK_PACKAGE_VERSION_ARRAY 2 STLINK_PACKAGE_VERSION_PATCH) +list(GET STLINK_PACKAGE_VERSION_ARRAY 3 STLINK_PACKAGE_VERSION_TAG) + if(WIN32) find_package(7Zip REQUIRED) message(STATUS "7Zip Location: " ${ZIP_LOCATION}) @@ -133,11 +148,30 @@ include_directories(${LIBUSB_INCLUDE_DIR}) include_directories(include) include_directories(src/mingw) -add_library(${PROJECT_NAME} STATIC +set(STLINK_LIB_STATIC ${PROJECT_NAME}-static) + +add_library(${PROJECT_NAME} SHARED ${STLINK_HEADERS} # header files for ide projects generated by cmake ${STLINK_SOURCE}) target_link_libraries(${PROJECT_NAME} ${LIBUSB_LIBRARY}) +if (WIN32 OR MSYS OR MINGW) + set(STLINK_SHARED_VERSION + ${STLINK_PACKAGE_VERSION_MAJOR}.${STLINK_PACKAGE_VERSION_MINOR}) +else() + set(STLINK_SHARED_VERSION + ${STLINK_PACKAGE_VERSION_MAJOR}.${STLINK_PACKAGE_VERSION_MINOR}.${STLINK_PACKAGE_VERSION_PATCH}) +endif() + +set_target_properties(${PROJECT_NAME} PROPERTIES SOVERSION ${STLINK_PACKAGE_VERSION_MAJOR} +VERSION ${STLINK_SHARED_VERSION}) + +add_library(${STLINK_LIB_STATIC} STATIC + ${STLINK_HEADERS} # header files for ide projects generated by cmake + ${STLINK_SOURCE}) +target_link_libraries(${STLINK_LIB_STATIC} ${LIBUSB_LIBRARY}) +set_target_properties(${STLINK_LIB_STATIC} PROPERTIES OUTPUT_NAME ${PROJECT_NAME}) + if (APPLE) find_library(ObjC objc) find_library(CoreFoundation CoreFoundation) @@ -163,10 +197,48 @@ else () target_link_libraries(st-util ${PROJECT_NAME}) endif () -install(TARGETS ${PROJECT_NAME} st-flash st-util st-info +install(TARGETS ${PROJECT_NAME} ${STLINK_LIB_STATIC} st-flash st-util st-info RUNTIME DESTINATION bin - ARCHIVE DESTINATION lib + ARCHIVE DESTINATION lib/${CMAKE_LIBRARY_PATH} + LIBRARY DESTINATION lib/${CMAKE_LIBRARY_PATH} ) +# Now, install the development headers +file(GLOB STLINK_HEADERS + "${CMAKE_SOURCE_DIR}/include/stlink/*.h" +) + +install(FILES ${CMAKE_SOURCE_DIR}/include/stlink.h + DESTINATION include/${CMAKE_LIBRARY_PATH}/stlink-${STLINK_PACKAGE_VERSION}/) + +install(FILES ${STLINK_HEADERS} + DESTINATION include/${CMAKE_LIBRARY_PATH}/stlink-${STLINK_PACKAGE_VERSION}/stlink) + +if (NOT APPLE AND NOT WIN32) + set(PKG_CONFIG_LIBDIR + "\${prefix}/lib/\${deb_host_multiarch}" + ) + set(PKG_CONFIG_INCLUDEDIR + "\${prefix}/include/\${deb_host_multiarch}/${PROJECT_NAME}-${STLINK_PACKAGE_VERSION}" + ) + set(PKG_CONFIG_LIBS + "-L\${libdir} -l:libstlink.so.${STLINK_PACKAGE_VERSION}" + ) + set(PKG_CONFIG_CFLAGS + "-I\${includedir}" + ) + + set(PKG_CONFIG_REQUIRES + "libusb-1.0" + ) + + configure_file( + "${CMAKE_CURRENT_SOURCE_DIR}/pkg-config.pc.cmake" + "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc" + ) + + install(FILES "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc" + DESTINATION lib/${CMAKE_LIBRARY_PATH}/pkgconfig/) +endif() if(NOT MINGW) list(APPEND STLINK_SOURCE src/tools/term.c) @@ -212,6 +284,6 @@ if (APPLE) set(CPACK_GENERATOR "ZIP") file(MAKE_DIRECTORY "${CMAKE_BINARY_DIR}/dist/osx") set(CPACK_OUTPUT_FILE_PREFIX "${CMAKE_BINARY_DIR}/dist/osx") -endif () +endif() add_custom_target(dist COMMAND ${CMAKE_MAKE_PROGRAM} package_source) include (CPack) diff --git a/debian/changelog b/debian/changelog new file mode 100644 index 0000000..d0471cb --- /dev/null +++ b/debian/changelog @@ -0,0 +1,5 @@ +libstlink (1.2.1) unstable; urgency=low + + * Initial Debian-Packaged Release. + + -- Andrew 'Necromant' Andrianov Sat, 09 Jul 2016 23:16:07 +0300 diff --git a/debian/compat b/debian/compat new file mode 100644 index 0000000..ec63514 --- /dev/null +++ b/debian/compat @@ -0,0 +1 @@ +9 diff --git a/debian/control b/debian/control new file mode 100644 index 0000000..b9beac2 --- /dev/null +++ b/debian/control @@ -0,0 +1,34 @@ +Source: libstlink +Priority: optional +Maintainer: Andrew 'Necromant' Andrianov +Build-Depends: debhelper (>= 9), cmake, libusb-1.0-0-dev, libgtk-3-dev +Standards-Version: 3.9.5 +Section: libs +Homepage: +Vcs-Git: https://github.com/texane/stlink.git +Vcs-Browser: https://github.com/texane/stlink + +Package: libstlink-dev +Section: libdevel +Architecture: any +Depends: libstlink (= ${binary:Version}), ${misc:Depends} +Description: OpenSource ST-Link tools replacement. Development headers. + This package contains development headers for libstlink. + +Package: libstlink +Architecture: any +Depends: ${shlibs:Depends}, ${misc:Depends} +Description: OpenSource ST-Link tools replacement. Shared library. + This package libstlink shared library. + +Package: stlink-tools +Section: libdevel +Architecture: any +Depends: libstlink (= ${binary:Version}), ${misc:Depends} +Description: OpenSource ST-Link tools replacement. Commandline Utilities. + +Package: stlink-gui +Section: libdevel +Architecture: any +Depends: libstlink (= ${binary:Version}), ${misc:Depends} +Description: OpenSource ST-Link tools replacement. GUI Tool. diff --git a/debian/libstlink-dev.dirs b/debian/libstlink-dev.dirs new file mode 100644 index 0000000..4418816 --- /dev/null +++ b/debian/libstlink-dev.dirs @@ -0,0 +1,2 @@ +usr/lib +usr/include diff --git a/debian/libstlink-dev.install b/debian/libstlink-dev.install new file mode 100644 index 0000000..1eff329 --- /dev/null +++ b/debian/libstlink-dev.install @@ -0,0 +1,3 @@ +usr/include/* +usr/lib/*/lib*.a +usr/lib/*/pkgconfig/* diff --git a/debian/libstlink.dirs b/debian/libstlink.dirs new file mode 100644 index 0000000..6845771 --- /dev/null +++ b/debian/libstlink.dirs @@ -0,0 +1 @@ +usr/lib diff --git a/debian/libstlink.install b/debian/libstlink.install new file mode 100644 index 0000000..703ad8b --- /dev/null +++ b/debian/libstlink.install @@ -0,0 +1 @@ +usr/lib/*/lib*.so* diff --git a/debian/rules b/debian/rules new file mode 100755 index 0000000..4979b59 --- /dev/null +++ b/debian/rules @@ -0,0 +1,28 @@ +#!/usr/bin/make -f +# See debhelper(7) (uncomment to enable) +# output every command that modifies files on the build system. +#DH_VERBOSE = 1 + +# see EXAMPLES in dpkg-buildflags(1) and read /usr/share/dpkg/* +DPKG_EXPORT_BUILDFLAGS = 1 +include /usr/share/dpkg/default.mk + +# see FEATURE AREAS in dpkg-buildflags(1) +#export DEB_BUILD_MAINT_OPTIONS = hardening=+all + +# see ENVIRONMENT in dpkg-buildflags(1) +# package maintainers to append CFLAGS +#export DEB_CFLAGS_MAINT_APPEND = -Wall -pedantic +# package maintainers to append LDFLAGS +#export DEB_LDFLAGS_MAINT_APPEND = -Wl,--as-needed + + +# main packaging script based on dh7 syntax +%: + dh $@ + +# debmake generated override targets +# This is example for Cmake (See http://bugs.debian.org/641051 ) +override_dh_auto_configure: + dh_auto_configure -- \ + -DCMAKE_LIBRARY_PATH=$(DEB_HOST_MULTIARCH) diff --git a/debian/source/format b/debian/source/format new file mode 100644 index 0000000..89ae9db --- /dev/null +++ b/debian/source/format @@ -0,0 +1 @@ +3.0 (native) diff --git a/debian/stlink-gui.dirs b/debian/stlink-gui.dirs new file mode 100644 index 0000000..d080ed4 --- /dev/null +++ b/debian/stlink-gui.dirs @@ -0,0 +1,2 @@ +/usr/bin +/usr/share/ diff --git a/debian/stlink-gui.install b/debian/stlink-gui.install new file mode 100644 index 0000000..eedcd71 --- /dev/null +++ b/debian/stlink-gui.install @@ -0,0 +1,2 @@ +/usr/bin/stlink-gui* +/usr/share/* diff --git a/debian/stlink-tools.dirs b/debian/stlink-tools.dirs new file mode 100644 index 0000000..377c766 --- /dev/null +++ b/debian/stlink-tools.dirs @@ -0,0 +1,2 @@ +/usr/bin + diff --git a/debian/stlink-tools.install b/debian/stlink-tools.install new file mode 100644 index 0000000..4fa1125 --- /dev/null +++ b/debian/stlink-tools.install @@ -0,0 +1 @@ +/usr/bin/st-* diff --git a/doc/app-example/CMakeLists.txt b/doc/app-example/CMakeLists.txt new file mode 100644 index 0000000..0e91bed --- /dev/null +++ b/doc/app-example/CMakeLists.txt @@ -0,0 +1,28 @@ +# Warning: This example assumes that you are building on a host +# with pkg-config available (e.g. linux). The logic required to +# build under windows/mingw and/or mac was intentionally omitted +# to keep this CMakeLists as small as possible + +cmake_minimum_required(VERSION 2.8) + +project(st-hello) +set(PROJECT_VERSION 0.1) +set(SRCS main.c) + +find_package(PkgConfig) +pkg_check_modules(STLINK REQUIRED stlink) + +set(CMAKE_C_FLAGS " ${STLINK_CFLAGS_OTHER} -Wall -Werror") + +include_directories( + ${STLINK_INCLUDE_DIRS} +) + +add_executable(${PROJECT_NAME} ${SRCS}) + +target_link_libraries(${PROJECT_NAME} + ${STLINK_LIBRARIES} +) + +install(TARGETS ${PROJECT_NAME} + DESTINATION bin) diff --git a/doc/app-example/README.md b/doc/app-example/README.md new file mode 100644 index 0000000..6830561 --- /dev/null +++ b/doc/app-example/README.md @@ -0,0 +1,2 @@ +This is a simple standalone application example that uses libstlink. +You can use this as a boilerplate for your own app development diff --git a/doc/app-example/main.c b/doc/app-example/main.c new file mode 100644 index 0000000..1262143 --- /dev/null +++ b/doc/app-example/main.c @@ -0,0 +1,29 @@ +#include +#include +#include + +static stlink_t *stlink_open_first(void) +{ + stlink_t* sl = NULL; + sl = stlink_v1_open(0, 1); + if (sl == NULL) + sl = stlink_open_usb(0, 1, NULL); + + return sl; +} + + +int main() +{ + stlink_t* sl = NULL; + sl = stlink_open_first(); + + if (sl == NULL) { + fprintf(stderr, "Failed to open stlink device ;(\n"); + exit(1); + } + + fprintf(stderr, "STlink device opened, that's cool!\n"); + stlink_close(sl); + return 0; +} diff --git a/pkg-config.pc.cmake b/pkg-config.pc.cmake new file mode 100644 index 0000000..4170bf8 --- /dev/null +++ b/pkg-config.pc.cmake @@ -0,0 +1,11 @@ +deb_host_multiarch=${CMAKE_LIBRARY_PATH} +Name: ${PROJECT_NAME} +Description: ${PROJECT_DESCRIPTION} +Version: ${PROJECT_VERSION} +Requires: ${PKG_CONFIG_REQUIRES} +prefix=${CMAKE_INSTALL_PREFIX} +includedir=${PKG_CONFIG_INCLUDEDIR} +libdir=${PKG_CONFIG_LIBDIR} +Libs: ${PKG_CONFIG_LIBS} +Cflags: ${PKG_CONFIG_CFLAGS} + diff --git a/scripts/run_clang_analyze.sh b/scripts/run_clang_analyze.sh new file mode 100755 index 0000000..8d5c5a9 --- /dev/null +++ b/scripts/run_clang_analyze.sh @@ -0,0 +1,8 @@ +#!/bin/bash +# Run this hacky script in project root directory to start +# clang static analysis. Adjust ccc-analyzer path if nesesary +CCC_ANALYZER=/usr/share/clang/scan-build-3.5/ccc-analyzer +mkdir -p build-clang-analyze/reports +cd build-clang-analyze +cmake -DCMAKE_C_COMPILER=${CCC_ANALYZER} $* .. +scan-build -o ./reports --keep-empty make