Skip to content

Commit

Permalink
Compile makefsdata utility from pico SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
krzmaz committed Oct 31, 2022
1 parent 883b1b6 commit 7bc32bf
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 21 deletions.
9 changes: 4 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
cmake_minimum_required(VERSION 3.19)

find_package(Perl)
if(NOT PERL_FOUND)
message(FATAL_ERROR "Perl is needed for generating the fsdata.c file")
endif()

set(PICO_BOARD pico_w)
include(cmake/utils.cmake)

Expand All @@ -16,6 +11,10 @@ set(PICO_SDK_FETCH_FROM_GIT on)
# note: this must happen before project()
include(cmake/pico_sdk_import.cmake)

if (NOT MAKEFSDATA_FOUND)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake)
find_package(MAKEFSDATA)
endif()

if(EXISTS cmake/credentials.cmake)
# copy it over from cmake/credentials.cmake.example
Expand Down
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ If you want to test this example with your html files, just add/edit them in the
### Dependencies:
- CMake 3.19+
- ARM GNU toolchain
- [Perl](https://www.perl.org/get.html) (should be already present on *nix systems)
- C++ toolchain for host architecture to build the makefsdata utility

For the first two you can refer to [these instructions](https://datasheets.raspberrypi.com/pico/getting-started-with-pico.pdf#%5B%7B%22num%22%3A39%2C%22gen%22%3A0%7D%2C%7B%22name%22%3A%22XYZ%22%7D%2C115%2C841.89%2Cnull%5D)

Expand All @@ -33,6 +33,12 @@ As per documentation:
> SSI-enabled pages must have one of the predefined SSI-enabled file extensions
Which means that no cost should be introduced in non SSI pages

## FS data
The `my_fsdata.c` file containing the HTML files is generated using [makefsdata](https://github.com/lwip-tcpip/lwip/tree/master/src/apps/http/makefsdata) executable compiled for the host machine.
It works on Mac in my testing, but the file only has `#ifdef` clauses for `__linux__` and `WIN32`,
so we trick it into compiling by defining `__linux__` on Mac, which is generally a bad idea, but it was
quicker to implement than creating patching logic.
### References
- https://www.nongnu.org/lwip/2_1_x/group__httpd.html
- https://github.com/lwip-tcpip/lwip/tree/master/contrib/examples/httpd
65 changes: 65 additions & 0 deletions cmake/FindMAKEFSDATA.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# Finds (or builds) the makefsdata executable
#
# This will define the following variables
#
# MAKEFSDATA_FOUND
#
# and the following imported targets
#
# MAKEFSDATA
#

if (NOT MAKEFSDATA_FOUND)

include(ExternalProject)
find_program(MAKE_EXECUTABLE NAMES gmake make mingw32-make REQUIRED)
# We need to use the main CMakeLists.txt from lwip, but we'll build only makefsdata
set(MAKEFSDATA_SOURCE_DIR ${PICO_SDK_PATH}/lib/lwip)
set(MAKEFSDATA_BINARY_DIR ${CMAKE_BINARY_DIR}/makefsdata)

set(MAKEFSDATA_BUILD_TARGET MAKEFSDATABuild)
set(MAKEFSDATA_TARGET makefsdata)

# horrible, horrible thing because makefsdata.c has a #ifdef __linux__ clause not caring about macs
if (APPLE)
message(WARNING "Setting up the linux define for apple, this might break things")
set(EXTRA_ARGS "-DCMAKE_C_FLAGS:STRING=\"-D__linux__\"")
endif()

if (NOT TARGET ${MAKEFSDATA_BUILD_TARGET})
ExternalProject_Add(${MAKEFSDATA_BUILD_TARGET}
PREFIX makefsdata SOURCE_DIR ${MAKEFSDATA_SOURCE_DIR}
BINARY_DIR ${MAKEFSDATA_BINARY_DIR}
BUILD_ALWAYS 1 # force dependency checking
CMAKE_ARGS ${EXTRA_ARGS}
BUILD_COMMAND ${CMAKE_COMMAND} --build ${MAKEFSDATA_BINARY_DIR} -t makefsdata
INSTALL_COMMAND ""
)
endif()

# depending on the platform the executable will be in different path
if(WIN32)
set(MAKEFSDATA_EXECUTABLE ${MAKEFSDATA_BINARY_DIR}/contrib/ports/win32/example_app/makefsdata)
get_target_property(TARGET_LIBRARIES ${MAKEFSDATA_BUILD_TARGET}::makefsdata LINK_LIBRARIES)
message("Libraries at start")
message(${TARGET_LIBRARIES})
LIST(REMOVE_ITEM TARGET_LIBRARIES lwipcontribportwindows )
message("Modified libraries list")
message(${TARGET_LIBRARIES})
set_property(TARGET ${MAKEFSDATA_BUILD_TARGET}::makefsdata PROPERTY LINK_LIBRARIES ${TARGET_LIBRARIES} )
get_target_property(TARGET_LIBRARIES2 ${MAKEFSDATA_BUILD_TARGET}::makefsdata LINK_LIBRARIES)
message("Libraries after change")
message(${TARGET_LIBRARIES2})
else()
set(MAKEFSDATA_EXECUTABLE ${MAKEFSDATA_BINARY_DIR}/contrib/ports/unix/example_app/makefsdata)
endif()

if(NOT TARGET ${MAKEFSDATA_TARGET})
add_executable(${MAKEFSDATA_TARGET} IMPORTED)
endif()
set_property(TARGET ${MAKEFSDATA_TARGET} PROPERTY IMPORTED_LOCATION
${MAKEFSDATA_EXECUTABLE})

add_dependencies(${MAKEFSDATA_TARGET} ${MAKEFSDATA_BUILD_TARGET})
set(MAKEFSDATA_FOUND 1)
endif()
19 changes: 4 additions & 15 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,26 +1,15 @@
set(PROGRAM_NAME pico_w_webserver)

set(MAKE_FS_DATA_SCRIPT ${CMAKE_CURRENT_LIST_DIR}/external/makefsdata)

if (NOT EXISTS ${MAKE_FS_DATA_SCRIPT})
file(DOWNLOAD
https://raw.githubusercontent.com/krzmaz/lwip/e15654409d14a238aec5ed4bd5516063938c9345/src/apps/http/makefsdata/makefsdata
${MAKE_FS_DATA_SCRIPT}
)
endif()
message("Running makefsdata script")
execute_process(COMMAND
perl ${MAKE_FS_DATA_SCRIPT}
add_custom_target(
generate_fs
COMMAND makefsdata -11 -f:my_fsdata.c
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
ECHO_OUTPUT_VARIABLE
ECHO_ERROR_VARIABLE
COMMAND_ERROR_IS_FATAL ANY
)
file(RENAME fsdata.c my_fsdata.c)

add_executable(${PROGRAM_NAME}
main.cpp
)
add_dependencies(${PROGRAM_NAME} generate_fs)
target_compile_definitions(${PROGRAM_NAME} PRIVATE
WIFI_SSID=\"${WIFI_SSID}\"
WIFI_PASSWORD=\"${WIFI_PASSWORD}\"
Expand Down

0 comments on commit 7bc32bf

Please sign in to comment.