diff --git a/piolib/CMakeLists.txt b/piolib/CMakeLists.txt index 2b437b9..a8ae476 100644 --- a/piolib/CMakeLists.txt +++ b/piolib/CMakeLists.txt @@ -1,11 +1,17 @@ cmake_minimum_required(VERSION 3.10...3.27) +#set project name +project(piolib) + +# After project(), so that the target architecture (and hence the Debian +# multiarch library directory) is known include(GNUInstallDirs) -add_subdirectory(examples) +# The examples are always built, but are only installed on request, so +# that a local install does not put them on everyone's PATH +option(PIOLIB_INSTALL_EXAMPLES "Install the piolib example programs" OFF) -#set project name -project(piolib) +add_subdirectory(examples) add_compile_definitions(LIBRARY_BUILD=1) @@ -34,7 +40,16 @@ set(INCLUDE_FILES "pico/stdlib.h" ) -install(TARGETS pio ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT piolib) +install(TARGETS pio + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT piolib + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} COMPONENT piolib) + +# pkg-config file; packagers can set PIOLIB_VERSION to the package version +set(PIOLIB_VERSION "0" CACHE STRING "Version reported by libpio.pc") +configure_file(libpio.pc.in libpio.pc @ONLY) +install(FILES "${CMAKE_CURRENT_BINARY_DIR}/libpio.pc" + DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig" COMPONENT piolib) + foreach ( file ${INCLUDE_FILES} ) get_filename_component( dir ${file} DIRECTORY ) install( FILES "include/${file}" DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/piolib/${dir}" COMPONENT piolib) diff --git a/piolib/README.md b/piolib/README.md index ddcff6e..6419630 100644 --- a/piolib/README.md +++ b/piolib/README.md @@ -8,6 +8,8 @@ Install the prerequisites with `sudo apt install build-essential cmake` - you ne 1. `cmake .` (or create a build subdirectory and run `cmake ..`) N.B. Use *cmake -DBUILD_SHARED_LIBS=1 .* to build piolib as a shared (as opposed to static) library. + The example programs are built but not installed; add *-DPIOLIB_INSTALL_EXAMPLES=ON* to install them too. + The default install prefix is /usr/local, which takes precedence over the packaged libpio; to try a build without displacing it, install to a private prefix (e.g. *-DCMAKE_INSTALL_PREFIX=$HOME/.local*) and point PKG_CONFIG_PATH and LD_LIBRARY_PATH at it. 2. `make` If `ls -l /dev/pio0` reports that the file is not found, you may need to update your Pi 5 firmware to one with PIO support and make sure that you are running a suitably recent kernel. diff --git a/piolib/examples/CMakeLists.txt b/piolib/examples/CMakeLists.txt index 7e40e9c..db72174 100644 --- a/piolib/examples/CMakeLists.txt +++ b/piolib/examples/CMakeLists.txt @@ -1,10 +1,16 @@ cmake_minimum_required(VERSION 3.10...3.27) -include(GNUInstallDirs) - #set project name project(examples) +include(GNUInstallDirs) + +function(install_example target) + if(PIOLIB_INSTALL_EXAMPLES) + install(TARGETS ${target} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} COMPONENT examples) + endif() +endfunction() + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -Wextra") option(ENABLE_WERROR "Treat compiler warnings as errors" OFF) if(ENABLE_WERROR) @@ -18,84 +24,84 @@ endif () add_executable(apitest apitest.c) target_include_directories(apitest PRIVATE ../include) target_link_libraries(apitest pio) -install(TARGETS apitest RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) +install_example(apitest) add_executable(piotest piotest.c) target_include_directories(piotest PRIVATE ../include) target_link_libraries(piotest pio) -install(TARGETS piotest RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) +install_example(piotest) add_executable(piopwm pwm.c) target_include_directories(piopwm PRIVATE ../include) target_link_libraries(piopwm pio) -install(TARGETS piopwm RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) +install_example(piopwm) add_executable(pioseq genseq.c) target_include_directories(pioseq PRIVATE ../include) target_link_libraries(pioseq pio) -install(TARGETS pioseq RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) +install_example(pioseq) add_executable(piows2812 ws2812.c) target_include_directories(piows2812 PRIVATE ../include) target_link_libraries(piows2812 pio) -install(TARGETS piows2812 RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) +install_example(piows2812) add_executable(dpi_csync dpi_csync.c) target_include_directories(dpi_csync PRIVATE ../include) target_link_libraries(dpi_csync pio) -install(TARGETS dpi_csync RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) +install_example(dpi_csync) add_executable(rp1sm rp1sm.c) target_include_directories(rp1sm PRIVATE ../include) target_link_libraries(rp1sm pio) -install(TARGETS rp1sm RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) +install_example(rp1sm) add_executable(quadenc quadrature_encoder.c) target_include_directories(quadenc PRIVATE ../include) target_link_libraries(quadenc pio) -install(TARGETS quadenc RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) +install_example(quadenc) add_executable(txcount txcount.c) target_include_directories(txcount PRIVATE ../include) target_link_libraries(txcount pio) -install(TARGETS txcount RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) +install_example(txcount) add_executable(blockingtest blockingtest.c) target_include_directories(blockingtest PRIVATE ../include) target_link_libraries(blockingtest pio pthread) -install(TARGETS blockingtest RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) +install_example(blockingtest) add_executable(rxsource rxsource.c) target_include_directories(rxsource PRIVATE ../include) target_link_libraries(rxsource pio) -install(TARGETS rxsource RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) +install_example(rxsource) add_executable(irqtest irqtest.c) target_include_directories(irqtest PRIVATE ../include ../../pinctrl) target_link_libraries(irqtest pio gpiolib) -install(TARGETS irqtest RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) +install_example(irqtest) add_executable(irqhog irqhog.c) target_include_directories(irqhog PRIVATE ../include) target_link_libraries(irqhog pio) -install(TARGETS irqhog RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) +install_example(irqhog) add_executable(irqtest2 irqtest2.c) target_include_directories(irqtest2 PRIVATE ../include ../../pinctrl) target_link_libraries(irqtest2 pio gpiolib) -install(TARGETS irqtest2 RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) +install_example(irqtest2) add_executable(txsink txsink.c) target_include_directories(txsink PRIVATE ../include) target_link_libraries(txsink pio) -install(TARGETS txsink RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) +install_example(txsink) add_executable(blink blink.c) target_include_directories(blink PRIVATE ../include) target_link_libraries(blink pio) -install(TARGETS blink RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) +install_example(blink) add_executable(mirror mirror.c) target_include_directories(mirror PRIVATE ../include) target_link_libraries(mirror pio) -install(TARGETS mirror RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) +install_example(mirror) diff --git a/piolib/libpio.pc.in b/piolib/libpio.pc.in new file mode 100644 index 0000000..b4a9ae4 --- /dev/null +++ b/piolib/libpio.pc.in @@ -0,0 +1,11 @@ +prefix=@CMAKE_INSTALL_PREFIX@ +exec_prefix=${prefix} +libdir=${exec_prefix}/@CMAKE_INSTALL_LIBDIR@ +includedir=${prefix}/@CMAKE_INSTALL_INCLUDEDIR@ + +Name: libpio +Description: Library for accessing Raspberry Pi 5 PIO hardware +Version: @PIOLIB_VERSION@ +Libs: -L${libdir} -lpio +Libs.private: -lpthread +Cflags: -I${includedir}/piolib