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

RFC: Build libstlink shared library, add debian packaging #444

Merged
merged 1 commit into from
Sep 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 78 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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
Expand All @@ -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})
Expand Down Expand Up @@ -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)
Expand All @@ -163,11 +197,49 @@ 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)
add_executable(st-term src/tools/term.c)
Expand Down Expand Up @@ -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)
5 changes: 5 additions & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
libstlink (1.2.1) unstable; urgency=low

* Initial Debian-Packaged Release.

-- Andrew 'Necromant' Andrianov <[email protected]> Sat, 09 Jul 2016 23:16:07 +0300
1 change: 1 addition & 0 deletions debian/compat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
9
34 changes: 34 additions & 0 deletions debian/control
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Source: libstlink
Priority: optional
Maintainer: Andrew 'Necromant' Andrianov <[email protected]>
Build-Depends: debhelper (>= 9), cmake, libusb-1.0-0-dev, libgtk-3-dev
Standards-Version: 3.9.5
Section: libs
Homepage: <insert the upstream URL, if relevant>
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.
2 changes: 2 additions & 0 deletions debian/libstlink-dev.dirs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
usr/lib
usr/include
3 changes: 3 additions & 0 deletions debian/libstlink-dev.install
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
usr/include/*
usr/lib/*/lib*.a
usr/lib/*/pkgconfig/*
1 change: 1 addition & 0 deletions debian/libstlink.dirs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
usr/lib
1 change: 1 addition & 0 deletions debian/libstlink.install
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
usr/lib/*/lib*.so*
28 changes: 28 additions & 0 deletions debian/rules
Original file line number Diff line number Diff line change
@@ -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)
1 change: 1 addition & 0 deletions debian/source/format
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
3.0 (native)
2 changes: 2 additions & 0 deletions debian/stlink-gui.dirs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/usr/bin
/usr/share/
2 changes: 2 additions & 0 deletions debian/stlink-gui.install
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/usr/bin/stlink-gui*
/usr/share/*
2 changes: 2 additions & 0 deletions debian/stlink-tools.dirs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/usr/bin

1 change: 1 addition & 0 deletions debian/stlink-tools.install
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/usr/bin/st-*
28 changes: 28 additions & 0 deletions doc/app-example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Copy link
Member

@xor-gate xor-gate Aug 29, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cmake_minimum_required should always be the first line for CMakeLists.txt

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cmake_minimum_required should always be the first line for CMakeLists.txt

It is there on the first line, diff just looks a bit weird due to adding sidenotes.


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)
2 changes: 2 additions & 0 deletions doc/app-example/README.md
Original file line number Diff line number Diff line change
@@ -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
29 changes: 29 additions & 0 deletions doc/app-example/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <stdio.h>
#include <stdlib.h>
#include <stlink.h>

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;
}
11 changes: 11 additions & 0 deletions pkg-config.pc.cmake
Original file line number Diff line number Diff line change
@@ -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}

8 changes: 8 additions & 0 deletions scripts/run_clang_analyze.sh
Original file line number Diff line number Diff line change
@@ -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