Skip to content

Commit 2668d9b

Browse files
committed
Standardise copying code and clean test environment
1 parent ee6e8a4 commit 2668d9b

File tree

2 files changed

+50
-28
lines changed

2 files changed

+50
-28
lines changed

scripts/genExamples.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import configparser
99
import platform
1010

11-
from pico_project import GenerateCMake
11+
from pico_project import GenerateCMake, copyExampleConfigs
1212

1313
# This script is designed to be run on Linux
1414
assert platform.system() == "Linux"
@@ -164,13 +164,16 @@
164164
os.system(
165165
f"git -c advice.detachedHead=false clone https://github.com/raspberrypi/pico-examples.git --depth=1 --branch {EXAMPLES_BRANCH}"
166166
)
167-
os.environ["PICO_SDK_PATH"] = f"~/.pico-sdk/sdk/{SDK_VERSION}"
168-
os.environ["WIFI_SSID"] = "Your Wi-Fi SSID"
169-
os.environ["WIFI_PASSWORD"] = "Your Wi-Fi Password"
170-
os.environ["TEST_TCP_SERVER_IP"] = (
171-
"192.168.1.100" # This isn't read from environment variables, so also needs to be passed to cmake
172-
)
173-
os.environ["MQTT_SERVER"] = "myMQTTserver"
167+
168+
PICO_SDK_PATH = f"~/.pico-sdk/sdk/{SDK_VERSION}"
169+
configure_env = {
170+
"PICO_SDK_PATH": PICO_SDK_PATH,
171+
"WIFI_SSID": "Your Wi-Fi SSID",
172+
"WIFI_PASSWORD": "Your Wi-Fi Password",
173+
"TEST_TCP_SERVER_IP": "192.168.1.100", # This isn't read from environment variables, so also needs to be passed to cmake
174+
"MQTT_SERVER": "myMQTTserver",
175+
}
176+
174177
os.environ["CFLAGS"] = "-Werror=cpp"
175178
os.environ["CXXFLAGS"] = "-Werror=cpp"
176179

@@ -187,12 +190,21 @@
187190
)
188191
toolchainPath = f"~/.pico-sdk/toolchain/{toolchainVersion}"
189192
picotoolDir = f"~/.pico-sdk/picotool/{SDK_VERSION}/picotool"
193+
194+
# Setup env to find targets
195+
for k, v in configure_env.items():
196+
os.environ[k] = v
197+
190198
os.system(
191199
f"cmake -S pico-examples -B build -DPICO_BOARD={board} -DPICO_PLATFORM={platform} -DPICO_TOOLCHAIN_PATH={toolchainPath} -Dpicotool_DIR={picotoolDir} -DTEST_TCP_SERVER_IP=$TEST_TCP_SERVER_IP"
192200
)
193201

194202
os.system("cmake --build build --target help > targets.txt")
195203

204+
# Clear env for clean tests
205+
for k in configure_env.keys():
206+
del os.environ[k]
207+
196208
targets = []
197209

198210
with open("targets.txt", "r") as f:
@@ -295,24 +307,12 @@ def test_build(target, v):
295307
"exampleLibs": v["libs"],
296308
}
297309
GenerateCMake(dir, params)
298-
if os.path.exists(f"{dir}/lwipopts.h"):
299-
with open(f"{dir}/lwipopts.h", "r") as f:
300-
if "lwipopts_examples_common.h" in f.read():
301-
# Write lwipopts for examples
302-
shutil.copy(
303-
f"{os.path.dirname(os.path.realpath(__file__))}/lwipopts.h",
304-
f"{dir}/lwipopts_examples_common.h",
305-
)
306-
307-
if os.path.exists(f"{dir}/mbedtls_config.h"):
308-
with open(f"{dir}/mbedtls_config.h", "r") as f:
309-
if "mbedtls_config_examples_common.h" in f.read():
310-
# Write mbedtls_config for examples
311-
shutil.copy(
312-
f"{os.path.dirname(os.path.realpath(__file__))}/mbedtls_config.h",
313-
f"{dir}/mbedtls_config_examples_common.h",
314-
)
315-
shutil.copy("pico-examples/pico_sdk_import.cmake", f"{dir}/")
310+
copyExampleConfigs(dir)
311+
312+
shutil.copy(
313+
os.path.expanduser(f"{PICO_SDK_PATH}/external/pico_sdk_import.cmake"),
314+
f"{dir}/",
315+
)
316316

317317
retcmake = os.system(f"cmake -S {dir} -B {dir}-build -GNinja")
318318
retbuild = os.system(f"cmake --build {dir}-build")

scripts/pico_project.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
import csv
2323
import json
2424

25+
sourcefolder = os.path.dirname(os.path.abspath(__file__))
26+
2527
CMAKELIST_FILENAME = "CMakeLists.txt"
2628
CMAKECACHE_FILENAME = "CMakeCache.txt"
2729

@@ -1322,6 +1324,28 @@ def generateProjectFiles(
13221324
os.chdir(oldCWD)
13231325

13241326

1327+
def copyExampleConfigs(projectPath):
1328+
lwipopts_path = os.path.join(projectPath, "lwipopts.h")
1329+
if os.path.exists(lwipopts_path):
1330+
with open(lwipopts_path, "r") as f:
1331+
if "lwipopts_examples_common.h" in f.read():
1332+
# Write lwipopts for examples
1333+
shutil.copy(
1334+
os.path.join(sourcefolder, "lwipopts.h"),
1335+
os.path.join(projectPath, "lwipopts_examples_common.h"),
1336+
)
1337+
1338+
mbedtls_config_path = os.path.join(projectPath, "mbedtls_config.h")
1339+
if os.path.exists(mbedtls_config_path):
1340+
with open(mbedtls_config_path, "r") as f:
1341+
if "mbedtls_config_examples_common.h" in f.read():
1342+
# Write mbedtls_config for examples
1343+
shutil.copy(
1344+
os.path.join(sourcefolder, "mbedtls_config.h"),
1345+
os.path.join(projectPath, "mbedtls_config_examples_common.h"),
1346+
)
1347+
1348+
13251349
def DoEverything(params):
13261350
if not os.path.exists(params["projectRoot"]):
13271351
print("Invalid project path")
@@ -1464,8 +1488,6 @@ def DoEverything(params):
14641488
# main execution starteth here
14651489

14661490
if __name__ == "__main__":
1467-
sourcefolder = os.path.dirname(os.path.abspath(__file__))
1468-
14691491
args = ParseCommandLine()
14701492

14711493
if args.nouart:

0 commit comments

Comments
 (0)