From 5b6bd40bc651678d20b98cd144c057d75a9961f6 Mon Sep 17 00:00:00 2001 From: "Michael (XIAO Xufeng)" Date: Wed, 14 Aug 2019 21:19:06 +0800 Subject: [PATCH] idf_py: support new command set-target Instead of using -DIDF_TARGET, this command is more intuitive: 1. Can limit the choice of targets 2. Easy to understand this is a destructive command 3. Easy to remember, and have an entry in the --help menu --- tools/ci/test_build_system_cmake.sh | 9 ++++ tools/idf.py | 68 ++++++++++++++++++++++------- 2 files changed, 61 insertions(+), 16 deletions(-) diff --git a/tools/ci/test_build_system_cmake.sh b/tools/ci/test_build_system_cmake.sh index bbe299b92..47e1ae23e 100755 --- a/tools/ci/test_build_system_cmake.sh +++ b/tools/ci/test_build_system_cmake.sh @@ -306,6 +306,15 @@ function run_tests() grep "CONFIG_IDF_TARGET=\"${fake_target}\"" sdkconfig || failure "Project not configured correctly using idf.py reconfigure -D" grep "IDF_TARGET:STRING=${fake_target}" build/CMakeCache.txt || failure "IDF_TARGET not set in CMakeCache.txt using idf.py reconfigure -D" + # TODO Change the real target to other value than esp32 when we have + real_target=esp32 + print_status "Can set target using idf.py set-target" + clean_build_dir + rm sdkconfig + idf.py set-target esp32 || failure "Failed to set target via idf.py set-target" + grep "CONFIG_IDF_TARGET=\"${real_target}\"" sdkconfig || failure "Project not configured correctly using idf.py set-target" + grep "IDF_TARGET:STRING=${real_target}" build/CMakeCache.txt || failure "IDF_TARGET not set in CMakeCache.txt using idf.py set-target" + # Clean up modifications for the fake target mv CMakeLists.txt.bak CMakeLists.txt rm -rf components diff --git a/tools/idf.py b/tools/idf.py index 0cad1edb1..3b911acc1 100755 --- a/tools/idf.py +++ b/tools/idf.py @@ -81,6 +81,8 @@ GENERATORS = [ GENERATOR_CMDS = dict((a[0], a[1]) for a in GENERATORS) GENERATOR_VERBOSE = dict((a[0], a[3]) for a in GENERATORS) +SUPPORTED_TARGETS = ["esp32"] + def _run_tool(tool_name, args, cwd): def quote_arg(arg): @@ -220,7 +222,6 @@ def _ensure_build_directory(args, always_run_cmake=False): os.makedirs(build_dir) cache_path = os.path.join(build_dir, "CMakeCache.txt") - args.define_cache_entry = list(args.define_cache_entry) args.define_cache_entry.append("CCACHE_ENABLE=%d" % args.ccache) if always_run_cmake or _new_cmakecache_entries(cache_path, args.define_cache_entry): @@ -408,6 +409,17 @@ def clean(action, ctx, args): build_target("clean", ctx, args) +def set_target(action, ctx, args, idf_target): + args.define_cache_entry.append("IDF_TARGET=" + idf_target) + sdkconfig_path = os.path.join(args.project_dir, 'sdkconfig') + sdkconfig_old = sdkconfig_path + ".old" + if os.path.exists(sdkconfig_old): + os.remove(sdkconfig_old) + if os.path.exists(sdkconfig_path): + os.rename(sdkconfig_path, sdkconfig_old) + print("Set Target to: %s, new sdkconfig created. Existing sdkconfig renamed to sdkconfig.old." % idf_target) + + def reconfigure(action, ctx, args): _ensure_build_directory(args, True) @@ -830,24 +842,21 @@ def init_cli(): for action_callback in ctx.command.global_action_callbacks: action_callback(ctx, global_args, tasks) - # very simple dependency management - completed_tasks = set() - if not tasks: print(ctx.get_help()) ctx.exit() - while tasks: - task = tasks[0] - tasks_dict = dict([(t.name, t) for t in tasks]) + # Make sure that define_cache_entry is list + global_args.define_cache_entry = list(global_args.define_cache_entry) - name_with_aliases = task.name - if task.aliases: - name_with_aliases += " (aliases: %s)" % ", ".join(task.aliases) - - ready_to_run = True + # Go through the task and create depended but missing tasks + all_tasks = [t.name for t in tasks] + tasks, tasks_to_handle = [], tasks + while tasks_to_handle: + task = tasks_to_handle.pop() + tasks.append(task) for dep in task.dependencies: - if dep not in completed_tasks: + if dep not in all_tasks: print( 'Adding %s\'s dependency "%s" to list of actions' % (task.name, dep) @@ -860,8 +869,20 @@ def init_cli(): if option and (option.scope.is_global or option.scope.is_shared): dep_task.action_args.pop(key) - tasks.insert(0, dep_task) - ready_to_run = False + tasks_to_handle.append(dep_task) + all_tasks.append(dep_task.name) + + # very simple dependency management + completed_tasks = set() + while tasks: + task = tasks[0] + tasks_dict = dict([(t.name, t) for t in tasks]) + + name_with_aliases = task.name + if task.aliases: + name_with_aliases += " (aliases: %s)" % ", ".join(task.aliases) + + ready_to_run = True for dep in task.order_dependencies: if dep in tasks_dict.keys() and dep not in completed_tasks: @@ -1082,7 +1103,22 @@ def init_cli(): + "For example, \"idf.py -DNAME='VALUE' reconfigure\" " + 'can be used to set variable "NAME" in CMake cache to value "VALUE".', "options": global_options, - "order_dependencies": ["menuconfig"], + "order_dependencies": ["menuconfig", "set-target", "fullclean"], + }, + "set-target": { + "callback": set_target, + "short_help": "Set the chip target to build.", + "help": "Set the chip target to build. This will remove the " + + "existing sdkconfig file and corresponding CMakeCache and " + + "create new ones according to the new target.\nFor example, " + + "\"idf.py set-target esp32\" will select esp32 as the new chip " + + "target.", + "arguments": [{ + "names": ["idf-target"], + "nargs": 1, + "type": click.Choice(SUPPORTED_TARGETS), + }], + "dependencies": ["fullclean", "reconfigure"], }, "clean": { "callback": clean,