Skip to content

Commit 162a728

Browse files
committed
Compile makefsdata utility from pico SDK
1 parent 883b1b6 commit 162a728

File tree

4 files changed

+80
-21
lines changed

4 files changed

+80
-21
lines changed

CMakeLists.txt

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
cmake_minimum_required(VERSION 3.19)
22

3-
find_package(Perl)
4-
if(NOT PERL_FOUND)
5-
message(FATAL_ERROR "Perl is needed for generating the fsdata.c file")
6-
endif()
7-
83
set(PICO_BOARD pico_w)
94
include(cmake/utils.cmake)
105

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

14+
if (NOT MAKEFSDATA_FOUND)
15+
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ${CMAKE_SOURCE_DIR}/cmake)
16+
find_package(MAKEFSDATA)
17+
endif()
1918

2019
if(EXISTS cmake/credentials.cmake)
2120
# copy it over from cmake/credentials.cmake.example

README.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ If you want to test this example with your html files, just add/edit them in the
88
### Dependencies:
99
- CMake 3.19+
1010
- ARM GNU toolchain
11-
- [Perl](https://www.perl.org/get.html) (should be already present on *nix systems)
11+
- C++ toolchain for host architecture to build the makefsdata utility
1212

1313
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)
1414

@@ -33,6 +33,12 @@ As per documentation:
3333
> SSI-enabled pages must have one of the predefined SSI-enabled file extensions
3434
3535
Which means that no cost should be introduced in non SSI pages
36+
37+
## FS data
38+
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.
39+
It works on Mac in my testing, but the file only has `#ifdef` clauses for `__linux__` and `WIN32`,
40+
so we trick it into compiling by defining `__linux__` on Mac, which is generally a bad idea, but it was
41+
quicker to implement than creating patching logic.
3642
### References
3743
- https://www.nongnu.org/lwip/2_1_x/group__httpd.html
3844
- https://github.com/lwip-tcpip/lwip/tree/master/contrib/examples/httpd

cmake/FindMAKEFSDATA.cmake

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# Finds (or builds) the makefsdata executable
2+
#
3+
# This will define the following variables
4+
#
5+
# MAKEFSDATA_FOUND
6+
#
7+
# and the following imported targets
8+
#
9+
# MAKEFSDATA
10+
#
11+
12+
if (NOT MAKEFSDATA_FOUND)
13+
14+
include(ExternalProject)
15+
find_program(MAKE_EXECUTABLE NAMES gmake make mingw32-make REQUIRED)
16+
# We need to use the main CMakeLists.txt from lwip, but we'll build only makefsdata
17+
set(MAKEFSDATA_SOURCE_DIR ${PICO_SDK_PATH}/lib/lwip)
18+
set(MAKEFSDATA_BINARY_DIR ${CMAKE_BINARY_DIR}/makefsdata)
19+
20+
set(MAKEFSDATA_BUILD_TARGET MAKEFSDATABuild)
21+
set(MAKEFSDATA_TARGET makefsdata)
22+
23+
# horrible, horrible thing because makefsdata.c has a #ifdef __linux__ clause not caring about macs
24+
if (APPLE)
25+
message(WARNING "Setting up the linux define for apple, this might break things")
26+
set(EXTRA_ARGS "-DCMAKE_C_FLAGS:STRING=\"-D__linux__\"")
27+
endif()
28+
29+
if (NOT TARGET ${MAKEFSDATA_BUILD_TARGET})
30+
ExternalProject_Add(${MAKEFSDATA_BUILD_TARGET}
31+
PREFIX makefsdata SOURCE_DIR ${MAKEFSDATA_SOURCE_DIR}
32+
BINARY_DIR ${MAKEFSDATA_BINARY_DIR}
33+
BUILD_ALWAYS 1 # force dependency checking
34+
CMAKE_ARGS ${EXTRA_ARGS}
35+
BUILD_COMMAND ${CMAKE_COMMAND} --build ${MAKEFSDATA_BINARY_DIR} -t makefsdata
36+
INSTALL_COMMAND ""
37+
)
38+
endif()
39+
40+
# depending on the platform the executable will be in different path
41+
if(WIN32)
42+
set(MAKEFSDATA_EXECUTABLE ${MAKEFSDATA_BINARY_DIR}/contrib/ports/win32/example_app/makefsdata)
43+
get_target_property(TARGET_LIBRARIES makefsdata LINK_LIBRARIES)
44+
message("Libraries at start")
45+
message(${TARGET_LIBRARIES})
46+
LIST(REMOVE_ITEM TARGET_LIBRARIES lwipcontribportwindows )
47+
message("Modified libraries list")
48+
message(${TARGET_LIBRARIES})
49+
set_property(TARGET makefsdata PROPERTY LINK_LIBRARIES ${TARGET_LIBRARIES} )
50+
get_target_property(TARGET_LIBRARIES2 makefsdata LINK_LIBRARIES)
51+
message("Libraries after change")
52+
message(${TARGET_LIBRARIES2})
53+
else()
54+
set(MAKEFSDATA_EXECUTABLE ${MAKEFSDATA_BINARY_DIR}/contrib/ports/unix/example_app/makefsdata)
55+
endif()
56+
57+
if(NOT TARGET ${MAKEFSDATA_TARGET})
58+
add_executable(${MAKEFSDATA_TARGET} IMPORTED)
59+
endif()
60+
set_property(TARGET ${MAKEFSDATA_TARGET} PROPERTY IMPORTED_LOCATION
61+
${MAKEFSDATA_EXECUTABLE})
62+
63+
add_dependencies(${MAKEFSDATA_TARGET} ${MAKEFSDATA_BUILD_TARGET})
64+
set(MAKEFSDATA_FOUND 1)
65+
endif()

src/CMakeLists.txt

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,15 @@
11
set(PROGRAM_NAME pico_w_webserver)
22

3-
set(MAKE_FS_DATA_SCRIPT ${CMAKE_CURRENT_LIST_DIR}/external/makefsdata)
4-
5-
if (NOT EXISTS ${MAKE_FS_DATA_SCRIPT})
6-
file(DOWNLOAD
7-
https://raw.githubusercontent.com/krzmaz/lwip/e15654409d14a238aec5ed4bd5516063938c9345/src/apps/http/makefsdata/makefsdata
8-
${MAKE_FS_DATA_SCRIPT}
9-
)
10-
endif()
11-
message("Running makefsdata script")
12-
execute_process(COMMAND
13-
perl ${MAKE_FS_DATA_SCRIPT}
3+
add_custom_target(
4+
generate_fs
5+
COMMAND makefsdata -11 -f:my_fsdata.c
146
WORKING_DIRECTORY ${CMAKE_CURRENT_LIST_DIR}
15-
ECHO_OUTPUT_VARIABLE
16-
ECHO_ERROR_VARIABLE
17-
COMMAND_ERROR_IS_FATAL ANY
187
)
19-
file(RENAME fsdata.c my_fsdata.c)
208

219
add_executable(${PROGRAM_NAME}
2210
main.cpp
2311
)
12+
add_dependencies(${PROGRAM_NAME} generate_fs)
2413
target_compile_definitions(${PROGRAM_NAME} PRIVATE
2514
WIFI_SSID=\"${WIFI_SSID}\"
2615
WIFI_PASSWORD=\"${WIFI_PASSWORD}\"

0 commit comments

Comments
 (0)