How to compile as library?
Closed this issue · 6 comments
Excuse my n00bness, but I'd like to figure out how to build this as a library.
I did manage to build the demo pdx form aarch64 sim already on my mac by using a simple
mdir build
cd build
cmake ../
make
To build as a lib I suppose I have to:
- change the include at the end of CmakeLists.txt from /playdate_game.cmake to playdate.cmake
- remove the occurrences of src/main.c from CmakeLists.txt
- ✅ Find a way how to set the TOOLCHAIN flag
For the TOOLCHAIN, I tried export TOOLCHAIN=armgcc
and repeating the above, but that seemed not to work. Also couldn't find any place in the repo where the variable is set. Probably this isn't an env var and I'm just oblivious to how cmake works.
edit: ok, I obviously should have read the Inside Playdate docs, TOOLCHAIN is set in buildsupport/arm.cmake and this is included like so:
cd build
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=<path to SDK>/C_API/buildsupport/arm.cmake ..
make
I could still use some tips / confirmation on my other points, though
OK, but why?
I'm interested in using HEBitmap in the Nim programming language. Instead of porting the source; compiling to a lib and writing bindings in Nim is customary
Doing the above (removing main.c and replacing playdate_game.cmake by playdate.cmake) seems to not be enough, as it still seems to be trying to build a pdx. What should I change to create an .a
static library?
❯ cd build
❯ cmake -DCMAKE_TOOLCHAIN_FILE=/Users/ninovanhooff/Developer/PlaydateSDK/C_API/buildsupport/arm.cmake -DCMAKE_BUILD_TYPE=Release ..
-- arm.cmake loaded..
-- arm.cmake loaded
-- The C compiler identification is GNU 9.2.1
-- The ASM compiler identification is GNU
-- Found assembler: /usr/local/bin/arm-none-eabi-gcc
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/local/bin/arm-none-eabi-gcc - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- SDK Path: /Users/ninovanhooff/Developer/PlaydateSDK
-- Configuring done (0.5s)
-- Generating done (0.0s)
-- Build files have been written to: /Users/ninovanhooff/PlaydateProjects/HEBitmap/build
❯ make
/opt/homebrew/Cellar/cmake/3.29.2/bin/cmake -S/Users/ninovanhooff/PlaydateProjects/HEBitmap -B/Users/ninovanhooff/PlaydateProjects/HEBitmap/build --check-build-system CMakeFiles/Makefile.cmake 0
/opt/homebrew/Cellar/cmake/3.29.2/bin/cmake -E cmake_progress_start /Users/ninovanhooff/PlaydateProjects/HEBitmap/build/CMakeFiles /Users/ninovanhooff/PlaydateProjects/HEBitmap/build//CMakeFiles/progress.marks
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Makefile2 all
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/HEBitmap_DEVICE.dir/build.make CMakeFiles/HEBitmap_DEVICE.dir/depend
cd /Users/ninovanhooff/PlaydateProjects/HEBitmap/build && /opt/homebrew/Cellar/cmake/3.29.2/bin/cmake -E cmake_depends "Unix Makefiles" /Users/ninovanhooff/PlaydateProjects/HEBitmap /Users/ninovanhooff/PlaydateProjects/HEBitmap /Users/ninovanhooff/PlaydateProjects/HEBitmap/build /Users/ninovanhooff/PlaydateProjects/HEBitmap/build /Users/ninovanhooff/PlaydateProjects/HEBitmap/build/CMakeFiles/HEBitmap_DEVICE.dir/DependInfo.cmake "--color="
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/HEBitmap_DEVICE.dir/build.make CMakeFiles/HEBitmap_DEVICE.dir/build
[ 25%] Building C object CMakeFiles/HEBitmap_DEVICE.dir/src/hebitmap.c.obj
/usr/local/bin/arm-none-eabi-gcc -DTARGET_EXTENSION=1 -DTARGET_PLAYDATE=1 -I/Users/ninovanhooff/Developer/PlaydateSDK/C_API -O3 -DNDEBUG -std=gnu11 -Wall -Wno-unknown-pragmas -Wdouble-promotion -O2 -mthumb -mcpu=cortex-m7 -mfloat-abi=hard -mfpu=fpv5-sp-d16 -D__FPU_USED=1 -falign-functions=16 -fomit-frame-pointer -gdwarf-2 -fverbose-asm -ffunction-sections -fdata-sections -mword-relocations -fno-common -MD -MT CMakeFiles/HEBitmap_DEVICE.dir/src/hebitmap.c.obj -MF CMakeFiles/HEBitmap_DEVICE.dir/src/hebitmap.c.obj.d -o CMakeFiles/HEBitmap_DEVICE.dir/src/hebitmap.c.obj -c /Users/ninovanhooff/PlaydateProjects/HEBitmap/src/hebitmap.c
[ 50%] Building C object CMakeFiles/HEBitmap_DEVICE.dir/src/hebitmap_prv.c.obj
/usr/local/bin/arm-none-eabi-gcc -DTARGET_EXTENSION=1 -DTARGET_PLAYDATE=1 -I/Users/ninovanhooff/Developer/PlaydateSDK/C_API -O3 -DNDEBUG -std=gnu11 -Wall -Wno-unknown-pragmas -Wdouble-promotion -O2 -mthumb -mcpu=cortex-m7 -mfloat-abi=hard -mfpu=fpv5-sp-d16 -D__FPU_USED=1 -falign-functions=16 -fomit-frame-pointer -gdwarf-2 -fverbose-asm -ffunction-sections -fdata-sections -mword-relocations -fno-common -MD -MT CMakeFiles/HEBitmap_DEVICE.dir/src/hebitmap_prv.c.obj -MF CMakeFiles/HEBitmap_DEVICE.dir/src/hebitmap_prv.c.obj.d -o CMakeFiles/HEBitmap_DEVICE.dir/src/hebitmap_prv.c.obj -c /Users/ninovanhooff/PlaydateProjects/HEBitmap/src/hebitmap_prv.c
[ 75%] Building C object CMakeFiles/HEBitmap_DEVICE.dir/Users/ninovanhooff/Developer/PlaydateSDK/C_API/buildsupport/setup.c.obj
/usr/local/bin/arm-none-eabi-gcc -DTARGET_EXTENSION=1 -DTARGET_PLAYDATE=1 -I/Users/ninovanhooff/Developer/PlaydateSDK/C_API -O3 -DNDEBUG -std=gnu11 -Wall -Wno-unknown-pragmas -Wdouble-promotion -O2 -mthumb -mcpu=cortex-m7 -mfloat-abi=hard -mfpu=fpv5-sp-d16 -D__FPU_USED=1 -falign-functions=16 -fomit-frame-pointer -gdwarf-2 -fverbose-asm -ffunction-sections -fdata-sections -mword-relocations -fno-common -MD -MT CMakeFiles/HEBitmap_DEVICE.dir/Users/ninovanhooff/Developer/PlaydateSDK/C_API/buildsupport/setup.c.obj -MF CMakeFiles/HEBitmap_DEVICE.dir/Users/ninovanhooff/Developer/PlaydateSDK/C_API/buildsupport/setup.c.obj.d -o CMakeFiles/HEBitmap_DEVICE.dir/Users/ninovanhooff/Developer/PlaydateSDK/C_API/buildsupport/setup.c.obj -c /Users/ninovanhooff/Developer/PlaydateSDK/C_API/buildsupport/setup.c
[100%] Linking C executable HEBitmap_DEVICE
/opt/homebrew/Cellar/cmake/3.29.2/bin/cmake -E cmake_link_script CMakeFiles/HEBitmap_DEVICE.dir/link.txt --verbose=1
/usr/local/bin/arm-none-eabi-gcc -O3 -DNDEBUG -nostartfiles -mthumb -mcpu=cortex-m7 -mfloat-abi=hard -mfpu=fpv5-sp-d16 -D__FPU_USED=1 -T/Users/ninovanhooff/Developer/PlaydateSDK/C_API/buildsupport/link_map.ld -Wl,-Map=game.map,--cref,--gc-sections,--no-warn-mismatch,--emit-relocs --entry eventHandlerShim CMakeFiles/HEBitmap_DEVICE.dir/src/hebitmap.c.obj CMakeFiles/HEBitmap_DEVICE.dir/src/hebitmap_prv.c.obj CMakeFiles/HEBitmap_DEVICE.dir/Users/ninovanhooff/Developer/PlaydateSDK/C_API/buildsupport/setup.c.obj -o HEBitmap_DEVICE
/usr/local/playdate/gcc-arm-none-eabi-9-2019-q4-major/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld: CMakeFiles/HEBitmap_DEVICE.dir/Users/ninovanhooff/Developer/PlaydateSDK/C_API/buildsupport/setup.c.obj: in function `eventHandlerShim':
/Users/ninovanhooff/Developer/PlaydateSDK/C_API/buildsupport/setup.c:15: undefined reference to `eventHandler'
/usr/local/playdate/gcc-arm-none-eabi-9-2019-q4-major/bin/../lib/gcc/arm-none-eabi/9.2.1/../../../../arm-none-eabi/bin/ld: /Users/ninovanhooff/Developer/PlaydateSDK/C_API/buildsupport/setup.c:15: undefined reference to `eventHandler'
collect2: error: ld returned 1 exit status
make[2]: *** [HEBitmap_DEVICE] Error 1
make[1]: *** [CMakeFiles/HEBitmap_DEVICE.dir/all] Error 2
make: *** [all] Error 2
Allright, I think I got it.
Apart from the above, I changed add_executable to add_library:
if (TOOLCHAIN STREQUAL "armgcc")
add_library(${PLAYDATE_GAME_DEVICE} STATIC src/hebitmap.c src/hebitmap_prv.c)
else()
add_library(${PLAYDATE_GAME_NAME} SHARED src/hebitmap.c src/hebitmap_prv.c)
endif()
And now I have a libHEBitmap_DEVICE.a
in the build folder 👍
When using this in Nim, I do get this warning while compiling, on an M1 Mac (arm64)
ld: warning: ignoring file '/Users/ninovanhooff/PlaydateProjects/wheelsprung.worktrees/hebitmap/lib/macos/libHEBitmap.dylib': found architecture 'arm64', required architecture 'x86_64'
Is this because it is trying to create a universal library (arm64 + x86_64)?
Ok, I think I'm done:
- changed sim library type to static
- added architectures for mac.
Full CmakeLists.txt becomes:
cmake_minimum_required(VERSION 3.14)
set(CMAKE_C_STANDARD 11)
set(ENVSDK $ENV{PLAYDATE_SDK_PATH})
if (NOT ${ENVSDK} STREQUAL "")
# Convert path from Windows
file(TO_CMAKE_PATH ${ENVSDK} SDK)
else()
execute_process(
COMMAND bash -c "egrep '^\\s*SDKRoot' $HOME/.Playdate/config"
COMMAND head -n 1
COMMAND cut -c9-
OUTPUT_VARIABLE SDK
OUTPUT_STRIP_TRAILING_WHITESPACE
)
endif()
if (NOT EXISTS ${SDK})
message(FATAL_ERROR "SDK Path not found; set ENV value PLAYDATE_SDK_PATH")
return()
endif()
set(CMAKE_CONFIGURATION_TYPES "Debug;Release")
set(CMAKE_XCODE_GENERATE_SCHEME TRUE)
# Game Name Customization
set(PLAYDATE_GAME_NAME HEBitmap)
set(PLAYDATE_GAME_DEVICE HEBitmap_DEVICE)
set(CMAKE_OSX_ARCHITECTURES "arm64;x86_64" CACHE STRING "" FORCE)
project(${PLAYDATE_GAME_NAME} C ASM)
if (TOOLCHAIN STREQUAL "armgcc")
add_library(${PLAYDATE_GAME_DEVICE} STATIC src/hebitmap.c src/hebitmap_prv.c)
else()
add_library(${PLAYDATE_GAME_NAME} STATIC src/hebitmap.c src/hebitmap_prv.c)
endif()
include(${SDK}/C_API/buildsupport/playdate.cmake)
Hey Nino, thanks for sharing the CMakeLists file.
Let me know if there are any issues with the library or Nim.
Thanks, it worked out and had a proof of concept working where /i could draw an image. I'm happy to see that it also works when the image is partially out of screen bounds