|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Arm Limited. All rights reserved. |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +package maker |
| 8 | + |
| 9 | +import ( |
| 10 | + "path" |
| 11 | + "path/filepath" |
| 12 | + "strings" |
| 13 | + |
| 14 | + "github.com/Open-CMSIS-Pack/cbuild2cmake/pkg/utils" |
| 15 | +) |
| 16 | + |
| 17 | +var WestToolchainMap = map[string]string{ |
| 18 | + "AC6": "armclang", |
| 19 | + "GCC": "gnuarmemb", |
| 20 | + "IAR": "iar", |
| 21 | + "CLANG": "llvm", |
| 22 | +} |
| 23 | + |
| 24 | +func (m *Maker) CreateWestCMakeLists(index int) error { |
| 25 | + cbuild := &m.Cbuilds[index] |
| 26 | + cbuild.ContextRoot, _ = filepath.Rel(m.SolutionRoot, cbuild.BaseDir) |
| 27 | + cbuild.ContextRoot = filepath.ToSlash(cbuild.ContextRoot) |
| 28 | + cbuild.Toolchain = m.RegisteredToolchains[m.SelectedToolchainVersion[index]].Name |
| 29 | + outDir := cbuild.AddRootPrefix(cbuild.ContextRoot, cbuild.BuildDescType.OutputDirs.Outdir) |
| 30 | + contextDir := path.Join(m.SolutionTmpDir, cbuild.BuildDescType.Context) |
| 31 | + westApp := cbuild.AddRootPrefix(cbuild.ContextRoot, cbuild.BuildDescType.West.AppPath) |
| 32 | + westToolchain := WestToolchainMap[cbuild.BuildDescType.Compiler] |
| 33 | + |
| 34 | + var westOptions, westDefs string |
| 35 | + var westOptionsRef, westDefsRef string |
| 36 | + for _, opt := range cbuild.BuildDescType.West.WestOpt { |
| 37 | + westOptions += "\n " + opt |
| 38 | + } |
| 39 | + if len(westOptions) > 0 { |
| 40 | + westOptions = "\nset(WEST_OPTIONS" + westOptions + "\n)" |
| 41 | + westOptionsRef = " ${WEST_OPTIONS}" |
| 42 | + } |
| 43 | + |
| 44 | + for _, define := range cbuild.BuildDescType.West.WestDefs { |
| 45 | + key, value := utils.GetDefine(define) |
| 46 | + def := key |
| 47 | + if len(value) > 0 { |
| 48 | + def += "=" + value |
| 49 | + } |
| 50 | + westDefs += "\n -D" + def |
| 51 | + } |
| 52 | + if len(westDefs) > 0 { |
| 53 | + westDefs = "\nset(WEST_DEFS" + westDefs + "\n)" |
| 54 | + westDefsRef = " -- ${WEST_DEFS}" |
| 55 | + } |
| 56 | + |
| 57 | + // Create toolchain.cmake |
| 58 | + err := m.CMakeCreateToolchain(index, contextDir, false) |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + |
| 63 | + // Create CMakeLists content |
| 64 | + content := `cmake_minimum_required(VERSION 3.27) |
| 65 | +
|
| 66 | +# Roots |
| 67 | +include("../roots.cmake") |
| 68 | +
|
| 69 | +set(CONTEXT ` + strings.ReplaceAll(cbuild.BuildDescType.Context, " ", "_") + `) |
| 70 | +set(TARGET ${CONTEXT}) |
| 71 | +set(OUT_DIR "` + outDir + `") |
| 72 | +set(WEST_BOARD "` + cbuild.BuildDescType.West.Board + `") |
| 73 | +set(WEST_APP "` + westApp + `") |
| 74 | +
|
| 75 | +# Toolchain config map |
| 76 | +include("toolchain.cmake") |
| 77 | +
|
| 78 | +# Setup project |
| 79 | +project(${CONTEXT} LANGUAGES NONE) |
| 80 | +` + westOptions + westDefs + ` |
| 81 | +
|
| 82 | +# Environment variables |
| 83 | +set(ZEPHYR_TOOLCHAIN_PATH "${REGISTERED_TOOLCHAIN_ROOT}/..") |
| 84 | +cmake_path(ABSOLUTE_PATH ZEPHYR_TOOLCHAIN_PATH NORMALIZE OUTPUT_VARIABLE ZEPHYR_TOOLCHAIN_PATH) |
| 85 | +set(ENV_VARS |
| 86 | + ` + strings.ToUpper(westToolchain) + `_TOOLCHAIN_PATH="${ZEPHYR_TOOLCHAIN_PATH}" |
| 87 | + ZEPHYR_TOOLCHAIN_VARIANT="` + westToolchain + `" |
| 88 | +) |
| 89 | +
|
| 90 | +# Compilation database |
| 91 | +add_custom_target(database |
| 92 | + COMMAND cmake -E env ${ENV_VARS} west build -b ${WEST_BOARD} -d "${OUT_DIR}" -p auto --cmake-only` + westOptionsRef + ` "${WEST_APP}"` + westDefsRef + ` |
| 93 | + USES_TERMINAL |
| 94 | +) |
| 95 | +
|
| 96 | +# West build |
| 97 | +add_custom_target(west |
| 98 | + COMMAND cmake -E env ${ENV_VARS} west build -b ${WEST_BOARD} -d "${OUT_DIR}" -p auto` + westOptionsRef + ` "${WEST_APP}"` + westDefsRef + ` |
| 99 | + USES_TERMINAL |
| 100 | +) |
| 101 | +` |
| 102 | + // Update CMakeLists.txt |
| 103 | + contextCMakeLists := path.Join(contextDir, "CMakeLists.txt") |
| 104 | + err = utils.UpdateFile(contextCMakeLists, content) |
| 105 | + if err != nil { |
| 106 | + return err |
| 107 | + } |
| 108 | + return err |
| 109 | +} |
0 commit comments