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

Compile makefsdata utility from pico SDK #4

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
7 changes: 3 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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")
if (NOT MAKEFSDATA_FOUND)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake)
find_package(MAKEFSDATA)
endif()

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


if(EXISTS cmake/credentials.cmake)
# copy it over from cmake/credentials.cmake.example
include(cmake/credentials.cmake)
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
58 changes: 58 additions & 0 deletions cmake/FindMAKEFSDATA.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# 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
GIT_REPOSITORY https://github.com/krzmaz/lwip.git
GIT_TAG origin/makefsdata-cmake-fix
GIT_SHALLOW true
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)
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