Skip to content

Commit ddc18c0

Browse files
committed
Pass source files to ESP-IDF build system via GLOB
This method is much more convenient for users as they won't need to edit CMakeLists.txt if a new file added to their project
1 parent 856885e commit ddc18c0

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

builder/frameworks/espidf.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -152,23 +152,30 @@ def collect_src_files():
152152
]
153153

154154

155+
def normalize_path(path):
156+
project_dir = env.subst("$PROJECT_DIR")
157+
if project_dir in path:
158+
path = path.replace(project_dir, "${CMAKE_SOURCE_DIR}")
159+
return to_unix_path(path)
160+
161+
155162
def create_default_project_files():
156163
root_cmake_tpl = """cmake_minimum_required(VERSION 3.16.0)
157164
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
158165
project(%s)
159166
"""
160-
prj_cmake_tpl = """# Warning! This code was automatically generated for projects
167+
prj_cmake_tpl = """# This file was automatically generated for projects
161168
# without default 'CMakeLists.txt' file.
162169
163-
set(app_sources
164-
%s)
170+
FILE(GLOB_RECURSE app_sources %s/*.*)
165171
166172
idf_component_register(SRCS ${app_sources})
167173
"""
168174

169175
if not listdir(join(env.subst("$PROJECT_SRC_DIR"))):
170-
# create an empty file to make CMake happy during first init
171-
open(join(env.subst("$PROJECT_SRC_DIR"), "empty.c"), "a").close()
176+
# create a default main file to make CMake happy during first init
177+
with open(join(env.subst("$PROJECT_SRC_DIR"), "main.c"), "w") as fp:
178+
fp.write("void app_main() {}")
172179

173180
project_dir = env.subst("$PROJECT_DIR")
174181
if not isfile(join(project_dir, "CMakeLists.txt")):
@@ -178,8 +185,10 @@ def create_default_project_files():
178185
project_src_dir = env.subst("$PROJECT_SRC_DIR")
179186
if not isfile(join(project_src_dir, "CMakeLists.txt")):
180187
with open(join(project_src_dir, "CMakeLists.txt"), "w") as fp:
181-
fp.write(prj_cmake_tpl % "".join(
182-
'\t"%s"\n' % to_unix_path(f) for f in collect_src_files()))
188+
fp.write(
189+
prj_cmake_tpl
190+
% normalize_path(env.subst("$PROJECT_SRC_DIR"))
191+
)
183192

184193

185194
def get_cmake_code_model(src_dir, build_dir, extra_args=None):

0 commit comments

Comments
 (0)