esp-idf icon indicating copy to clipboard operation
esp-idf copied to clipboard

Overwrite SDKConfig variable in cmakelist,txt (IDFGH-13356)

Open ioviedodev opened this issue 1 year ago • 3 comments

Is your feature request related to a problem?

No response

Describe the solution you'd like.

It would be great in Cmakelist file could rewrite a SDKconfig variable, for example CONFIG_PARTITION_TABLE_CUSTOM_FILENAME.

Describe alternatives you've considered.

No response

Additional context.

No response

ioviedodev avatar Jul 29 '24 22:07 ioviedodev

Hi @ioviedodev,

You can use sdkconfig.defaults file to place the config options you wish to modify from their default values See the documentation here.

The sdkconfig file is used to generate sdkconfig.h containing macros of the config options. Trying to change their values in CMakelists would mean redefining the macros.

SoucheSouche avatar Jul 30 '24 11:07 SoucheSouche

Hi @SoucheSouche , thank you for your reply so I have the following scenario.

I create a kconfig variable CONFIG_OPTION // could have 0,1 This option i could see in Sdkconfig

and when I choose if OPTION=0 CONFIG_PARTITION_TABLE_CUSTOM_FILENAME= "path1" else CONFIG_PARTITION_TABLE_CUSTOM_FILENAME= "path2"

ioviedodev avatar Jul 30 '24 14:07 ioviedodev

In that case I would just create 2 specific sdkconfig.defaults files (sdkconfig.defaults.option_0 and sdkconfig.defaults.option_1) And then apply one or the other to the sdkconfig file before building the application.

But what you describe doesn't seem to be possible since you can't redefine the macros in CMakefiles.

SoucheSouche avatar Jul 31 '24 06:07 SoucheSouche

@ioviedodev

Add the file Kconfig.projbuild to the same scope as the file with app_main().

menu "Ioviedodev App Configuration"

	choice BUILD_TYPE
		prompt "Build Type"
		default BUILD_DEVELOPMENT

        config BUILD_DEVELOPMENT
            bool "Development"
        config BUILD_PRODUCTION
            bool "Production"
	endchoice

endmenu

In the root CMakeLists.txt add the below before calling project(whatever_app_name)

find_package(Python3 REQUIRED COMPONENTS Interpreter)
set(python ${Python3_EXECUTABLE})
execute_process(COMMAND ${python} "${CMAKE_CURRENT_SOURCE_DIR}/.tools/prebuild.py")

Create a directory in root ./tools and create the file prebuild.py.

In prebuild.py:

import sys
import os

def copy_and_replace(src_file):
    with open(src_file, 'r') as file:
        data = file.read()
    with open("../sdkconfig.defaults", 'w') as file:
        file.write(data)

def isDev():
	if not os.path.exists("../sdkconfig"):
		print ('sdkconfig not found. Defaulting build type!')
		print ('Build Type: Development')
		copy_and_replace('../sdkconfig.defaults-dev')
	else:
		with open("../sdkconfig", 'r') as f:
			if 'CONFIG_BUILD_DEVELOPMENT=y' in f.read():
				print ('Build Type: Development')
				copy_and_replace('../sdkconfig.defaults-dev')
			else:
				print ('Build Type: Production')
				copy_and_replace('../sdkconfig.defaults-prod')
isDev()

Now create the files sdkconfig.defaults-dev, sdkconfig.defaults-prod, and if you haven't already, create sdkconfig.defaults.

In sdkconfig.defaults-dev:

CONFIG_PARTITION_TABLE_CUSTOM_FILENAME= "path1"

In sdkconfig.defaults-prod:

CONFIG_PARTITION_TABLE_CUSTOM_FILENAME= "path2"

DarmorGamz avatar Aug 02 '24 21:08 DarmorGamz

Hello @ioviedodev, should I consider this issue resolved or is there anything else we can help you with?

SoucheSouche avatar Aug 09 '24 08:08 SoucheSouche

Closing this one then, @ioviedodev feel free to reopen if you still think the suggested solution do not meet your requirements.

ESP-Marius avatar Aug 15 '24 04:08 ESP-Marius

Hi, thanks a lot. Your help me so much, @DarmorGamz and @SoucheSouche. I resolved it.

ioviedodev avatar Aug 22 '24 15:08 ioviedodev