Optimization flags

Hi,

I was wondering, where should I put the optimization flags in the MakeFile, when working in C. Do these go under CDEFINES=?

-DNDEBUG -O2 -g0 -s

Thank you for your help :slight_smile:

I don’t have the SDK available to me right now, but I remember it being straight forward to find this answer by looking inside the master Makefile that comes with the SDK. All the examples refer to it.

Which flag would it be? I'm not too familiar with MakeFiles

#
# CMake include file for Playdate libraries
#

cmake_minimum_required(VERSION 3.19)

set(CMAKE_VERBOSE_MAKEFILE TRUE)

include_directories("${SDK}/C_API")
message(STATUS "SDK Path: " ${SDK})

set(PDC "${SDK}/bin/pdc" -sdkpath "${SDK}")

add_compile_definitions(TARGET_EXTENSION=1)

if (TOOLCHAIN STREQUAL "armgcc")
	# Device-only

	# Glue code
	target_sources(${PLAYDATE_GAME_DEVICE} PRIVATE ${SDK}/C_API/buildsupport/setup.c)

	set(HEAP_SIZE 8388208)
	set(STACK_SIZE 61800)
	set(CMAKE_ASM_FLAGS "${CMAKE_ASM_FLAGS} -x assembler-with-cpp -D__HEAP_SIZE=${HEAP_SIZE} -D__STACK_SIZE=${STACK_SIZE}")

	set(MCFLAGS -mthumb -mcpu=cortex-m7 -mfloat-abi=hard -mfpu=fpv5-sp-d16 -D__FPU_USED=1)

	target_compile_definitions(${PLAYDATE_GAME_DEVICE} PUBLIC TARGET_PLAYDATE=1)
	target_compile_options(${PLAYDATE_GAME_DEVICE} PUBLIC -Wall -Wno-unknown-pragmas -Wdouble-promotion)
	target_compile_options(${PLAYDATE_GAME_DEVICE} PUBLIC $<$<CONFIG:DEBUG>:-O2>)
	target_compile_options(${PLAYDATE_GAME_DEVICE} PUBLIC $<$<CONFIG:RELEASE>:-O2>)

	target_compile_options(${PLAYDATE_GAME_DEVICE} PUBLIC ${MCFLAGS})
	target_compile_options(${PLAYDATE_GAME_DEVICE} PUBLIC -falign-functions=16 -fomit-frame-pointer)
	target_compile_options(${PLAYDATE_GAME_DEVICE} PUBLIC -gdwarf-2)
	target_compile_options(${PLAYDATE_GAME_DEVICE} PUBLIC -fverbose-asm)
	target_compile_options(${PLAYDATE_GAME_DEVICE} PUBLIC -ffunction-sections -fdata-sections)

    target_compile_options(${PLAYDATE_GAME_DEVICE} PUBLIC $<$<COMPILE_LANGUAGE:CXX>:-fno-exceptions>)

	target_link_options(${PLAYDATE_GAME_DEVICE} PUBLIC ${MCFLAGS})
	target_link_options(${PLAYDATE_GAME_DEVICE} PUBLIC -T${SDK}/C_API/buildsupport/link_map.ld)
	target_link_options(${PLAYDATE_GAME_DEVICE} PUBLIC "-Wl,-Map=game.map,--cref,--gc-sections,--no-warn-mismatch")

else ()
	# Simulator-only
	target_compile_definitions(${PLAYDATE_GAME_NAME} PUBLIC TARGET_SIMULATOR=1)
	if (MSVC)
		target_compile_definitions(${PLAYDATE_GAME_NAME} PUBLIC _WINDLL=1)
		target_compile_options(${PLAYDATE_GAME_NAME} PUBLIC /W3)
		target_compile_options(${PLAYDATE_GAME_NAME} PUBLIC $<$<CONFIG:DEBUG>:/Od>)
	else()
		target_compile_options(${PLAYDATE_GAME_NAME} PUBLIC -Wall -Wstrict-prototypes -Wno-unknown-pragmas -Wdouble-promotion)
		target_compile_options(${PLAYDATE_GAME_NAME} PUBLIC $<$<CONFIG:DEBUG>:-ggdb -O0>)
	endif()

endif ()

check C_FLAGS values in CMakeCache.txt. You'll notice there are multiple as you can switch between debug release etc using cmake. Todo that you just run cmake again with for example -DCMAKE_BUILD_TYPE=Release

The file you want is sdk/C_API/buildsupport/common.mk, and the variable is called CPFLAGS. You should be able to add optimization configs there, but maybe don't remove any of the flags the makefile already defines.

@joyrider3774 In the docs:

Building for Release

When you’re ready to do a release build, regenerate the build targets by passing -DCMAKE_BUILD_TYPE=Release argument to CMake.

I'm not familiar with cmake, I'm using a plain MakeFile, is there a way for me to specify making a release build from this?

Edit

I've learnt cmake, made everything a whole lot easier.
Thanks everyone.

2 Likes

In answer to the original question, there's no official way of passing compiler arguments from your user Makefile to buildsupport/common.mk. However, there's nothing stoping you from adding them to UDEFS. As previously noted, using CMake is the solution if you want to get fancy.