Skip to content

Commit

Permalink
Add standalone/static_sdk helper for figuring out compile/link args f…
Browse files Browse the repository at this point in the history
…or external build including SDK code
  • Loading branch information
kilograham committed Feb 1, 2021
1 parent d157b5c commit b4130d0
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 0 deletions.
2 changes: 2 additions & 0 deletions standalone/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Directory of standalone projects; i.e. they are not included in the main build.
You should do a cmake build from within them
94 changes: 94 additions & 0 deletions standalone/static_sdk/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# THIS IS A SLIGHTLY HACKY EXAMPLE PROJECT DESIGNED TO HELP YOU TOWARDS INCLUDING THE PICO SDK
# CODE OUTSIDE OF THE CMAKE BUILD
#
# Note that the Pico SDK is designed to make it easy for users to build applications rather to build
# other runtimes for the Pico.
#
# There is no included "static library" of the Pico SDK because all of the Pico SDK Libraries are configurable
# on a per binary basis, and are always compiled from scratch per binary within the normal Pico SDK CMake build
#
# Note you would want to customize this to add or remove the dependencies you want (e.g. you mau not want
# to depend on pico_stdlib, but rather pick and mix some of its constituents to give you a finer grain of control
#
# Note that this example produces a build_info.txt in the output with some approximate (we're not trying to
# rewrite CMake here!) compile and link flags based on the setup of your static_sdk library
#
# Finally note that even copying the build and link flags from build_info.txt and linking against the generated
# library does not get you all the way, as you still need a second stage boot at the start of the binary (0x000-0x100)

cmake_minimum_required(VERSION 3.12)

# Pull in PICO SDK (must be before project)
include(pico_sdk_import.cmake)

project(static_sdk C CXX)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 17)

pico_sdk_init()

if (PICO_ON_DEVICE)
include(hacky_cmake_helper.cmake)

add_library(static_sdk
dummy.c)

target_compile_definitions(static_sdk PUBLIC
PICO_NO_BUILD_TYPE_FEATURE=1)

target_link_libraries(static_sdk PUBLIC pico_stdlib)

# our hacky_cmake_helper does not deal with generator expressions for libraries, so pick some (assuming we want the pico versions)
# you might choose not to have these libraries (which are mostly redirecting AEABI functions to our faster (possible ROM) equivalents
# especially if you are skipping SDK initialization (which is required for some of them)
target_link_libraries(static_sdk PUBLIC
pico_printf_pico
pico_bit_ops_pico
pico_divider_hardware
pico_float_pico
pico_double_pico
pico_mem_ops_pico
pico_int64_ops_pico

pico_stdio_uart
#pico_stdio_usb
)

# Compile definitions
gather_vars(COMPILE_DEFINITIONS INTERFACE_COMPILE_DEFINITIONS _touched_cd static_sdk)
list(REMOVE_DUPLICATES COMPILE_DEFINITIONS)
List(PREPEND COMPILE_DEFINITIONS "") # need a -D at the beginning
string(REPLACE ";" " -D" COMPILE_DEFINITIONS "${COMPILE_DEFINITIONS}")

# Compile options
gather_vars(COMPILE_OPTIONS INTERFACE_COMPILE_OPTIONS _touched_cf static_sdk)
list(REMOVE_DUPLICATES COMPILE_OPTIONS)
string(REPLACE ";" " " COMPILE_OPTIONS "${COMPILE_OPTIONS}")
# Note that the Pico SDK has some options specific to the language being compiled. All
# SDK has already been compiled, so we'll just strip these and you'll have to deal with these yourselves
string(REGEX REPLACE "\\$<\\$<COMPILE_LANGUAGE:CXX>:.*>" "" COMPILE_OPTIONS ${COMPILE_OPTIONS})

# Include dirs
gather_vars(INCLUDE_DIRECTORIES INTERFACE_INCLUDE_DIRECTORIES _touched_id static_sdk)
list(REMOVE_DUPLICATES INCLUDE_DIRECTORIES)
string(REPLACE ";" " " INCLUDE_DIRECTORIES "${INCLUDE_DIRECTORIES}")

# Link options
gather_vars(LINK_FLAGS INTERFACE_LINK_OPTIONS _touched_l static_sdk)
string(REPLACE ";" " " LINK_FLAGS "${LINK_FLAGS}")
string(REPLACE "LINKER:" "-Wl," LINK_FLAGS "${LINK_FLAGS}")

# Target properties were scoped to the target being built, lets scope them to static_sdk
string(REPLACE "$<TARGET_PROPERTY:" "$<TARGET_PROPERTY:static_sdk," COMPILE_DEFINITIONS "${COMPILE_DEFINITIONS}")
string(REPLACE "$<TARGET_PROPERTY:" "$<TARGET_PROPERTY:static_sdk," COMPILE_OPTIONS "${COMPILE_OPTIONS}")
string(REPLACE "$<TARGET_PROPERTY:" "$<TARGET_PROPERTY:static_sdk," INCLUDE_DIRECTORIES "${INCLUDE_DIRECTORIES}")
string(REPLACE "$<TARGET_PROPERTY:" "$<TARGET_PROPERTY:static_sdk," LINK_FLAGS "${LINK_FLAGS}")

# The only way to get CMake to do some generator expression evaluation is to generate a file
file(GENERATE OUTPUT build_info.txt CONTENT
"Compile definitions: ${COMPILE_DEFINITIONS}
Compile options : ${COMPILE_OPTIONS}
Include dirs : ${INCLUDE_DIRECTORIES}
Link flags : ${LINK_FLAGS}")

endif()
Empty file added standalone/static_sdk/dummy.c
Empty file.
27 changes: 27 additions & 0 deletions standalone/static_sdk/hacky_cmake_helper.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
function(gather_vars VAR_NAME PROPERTY_NAME INCLUDED_VAR_NAME LIB)
#message("${VAR_NAME}:${PROPERTY_NAME}:${INCLUDED_VAR_NAME}:${LIB}")
if (NOT ${LIB} IN_LIST ${INCLUDED_VAR_NAME})
string(FIND "${LIB}" "$<" HAS_GENERATOR_EXPRESSIONS)
string(FIND "${LIB}" "$<" HAS_GENERATOR_EXPRESSIONS)
if (LIB MATCHES "\\$<")
message("Skipping generator expression in ${VAR_NAME}: ${LIB}")
elseif (LIB MATCHES "::@")
# todo what are these?
else()
list(APPEND ${INCLUDED_VAR_NAME} ${LIB})
set(${INCLUDED_VAR_NAME} ${${INCLUDED_VAR_NAME}} PARENT_SCOPE)
get_target_property(new_items ${LIB} ${PROPERTY_NAME})
if (new_items)
list(APPEND ${VAR_NAME} ${new_items})
endif()
get_target_property(trans_depend ${LIB} INTERFACE_LINK_LIBRARIES)
# message("Tdep ${trans_depend}")
if (trans_depend)
foreach(SUB_LIB ${trans_depend})
gather_vars(${VAR_NAME} ${PROPERTY_NAME} ${INCLUDED_VAR_NAME} ${SUB_LIB})
endforeach()
endif()
set(${VAR_NAME} ${${VAR_NAME}} PARENT_SCOPE)
endif()
endif()
endfunction()
62 changes: 62 additions & 0 deletions standalone/static_sdk/pico_sdk_import.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# This is a copy of <PICO_SDK_PATH>/external/pico_sdk_import.cmake

# This can be dropped into an external project to help locate this SDK
# It should be include()ed prior to project()

if (DEFINED ENV{PICO_SDK_PATH} AND (NOT PICO_SDK_PATH))
set(PICO_SDK_PATH $ENV{PICO_SDK_PATH})
message("Using PICO_SDK_PATH from environment ('${PICO_SDK_PATH}')")
endif ()

if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT} AND (NOT PICO_SDK_FETCH_FROM_GIT))
set(PICO_SDK_FETCH_FROM_GIT $ENV{PICO_SDK_FETCH_FROM_GIT})
message("Using PICO_SDK_FETCH_FROM_GIT from environment ('${PICO_SDK_FETCH_FROM_GIT}')")
endif ()

if (DEFINED ENV{PICO_SDK_FETCH_FROM_GIT_PATH} AND (NOT PICO_SDK_FETCH_FROM_GIT_PATH))
set(PICO_SDK_FETCH_FROM_GIT_PATH $ENV{PICO_SDK_FETCH_FROM_GIT_PATH})
message("Using PICO_SDK_FETCH_FROM_GIT_PATH from environment ('${PICO_SDK_FETCH_FROM_GIT_PATH}')")
endif ()

set(PICO_SDK_PATH "${PICO_SDK_PATH}" CACHE PATH "Path to the PICO SDK")
set(PICO_SDK_FETCH_FROM_GIT "${PICO_SDK_FETCH_FROM_GIT}" CACHE BOOL "Set to ON to fetch copy of PICO SDK from git if not otherwise locatable")
set(PICO_SDK_FETCH_FROM_GIT_PATH "${PICO_SDK_FETCH_FROM_GIT_PATH}" CACHE FILEPATH "location to download SDK")

if (NOT PICO_SDK_PATH)
if (PICO_SDK_FETCH_FROM_GIT)
include(FetchContent)
set(FETCHCONTENT_BASE_DIR_SAVE ${FETCHCONTENT_BASE_DIR})
if (PICO_SDK_FETCH_FROM_GIT_PATH)
get_filename_component(FETCHCONTENT_BASE_DIR "${PICO_SDK_FETCH_FROM_GIT_PATH}" REALPATH BASE_DIR "${CMAKE_SOURCE_DIR}")
endif ()
FetchContent_Declare(
pico_sdk
GIT_REPOSITORY https://github.com/raspberrypi/pico-sdk
GIT_TAG master
)
if (NOT pico_sdk)
message("Downloading PICO SDK")
FetchContent_Populate(pico_sdk)
set(PICO_SDK_PATH ${pico_sdk_SOURCE_DIR})
endif ()
set(FETCHCONTENT_BASE_DIR ${FETCHCONTENT_BASE_DIR_SAVE})
else ()
message(FATAL_ERROR
"PICO SDK location was not specified. Please set PICO_SDK_PATH or set PICO_SDK_FETCH_FROM_GIT to on to fetch from git."
)
endif ()
endif ()

get_filename_component(PICO_SDK_PATH "${PICO_SDK_PATH}" REALPATH BASE_DIR "${CMAKE_BINARY_DIR}")
if (NOT EXISTS ${PICO_SDK_PATH})
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' not found")
endif ()

set(PICO_SDK_INIT_CMAKE_FILE ${PICO_SDK_PATH}/pico_sdk_init.cmake)
if (NOT EXISTS ${PICO_SDK_INIT_CMAKE_FILE})
message(FATAL_ERROR "Directory '${PICO_SDK_PATH}' does not appear to contain the PICO SDK")
endif ()

set(PICO_SDK_PATH ${PICO_SDK_PATH} CACHE PATH "Path to the PICO SDK" FORCE)

include(${PICO_SDK_INIT_CMAKE_FILE})

0 comments on commit b4130d0

Please sign in to comment.