#!/bin/bash # Copyright 2020 The Chromium OS Authors. All rights reserved. # Use of this source code is governed by a BSD-style license that can be # found in the LICENSE file. # # Usage: ./util/ide-config.sh vscode all all:RO | tee .vscode/c_cpp_properties.json # This tool needs to be run from the base ec directory. # # Future works should be put towards adding new IDE generators and adding # mechanism for passing IDE specific parameters to the IDE generator. DEFAULT_IMAGE=RW INDENT_WIDTH=${INDENT_WIDTH:-1} INDENT_CHAR=${INDENT_CHAR:-$'\t'} INCLUDES_DROP_ROOT=${INCLUDES_DROP_ROOT:-false} PARALLEL_CACHE_FILL=${PARALLEL_CACHE_FILL:-true} FORCE_INCLUDE_CONFIG_H=${FORCE_INCLUDE_CONFIG_H:-false} # JOB_BATCH_SIZE is the number of jobs allowed to spawn at any given point. # This is an inefficient manner of resource management, but at least it # throttles process creation. Due to the low utilization or each job, # multiply the number of real processors by 2. JOB_BATCH_SIZE=${JOB_BATCH_SIZE:-$(($(nproc) * 2))} MAKE_CACHE_DIR="" init() { MAKE_CACHE_DIR=$(mktemp -t -d ide-config.XXXX) mkdir -p "${MAKE_CACHE_DIR}/defines" mkdir -p "${MAKE_CACHE_DIR}/includes" trap deinit EXIT } deinit() { rm -rf "${MAKE_CACHE_DIR}" } usage() { cat <<-HEREDOC Usage: ide-config.sh [BOARD:IMAGE] [BOARD:IMAGE...] Generate a C language configuration for a given IDE and EC board. Examples: ide-config.sh vscode all:RW all:RO | tee .vscode/c_cpp_properties.json ide-config.sh vscode nocturne # implicitly :RW ide-config.sh vscode nocturne_fp:RO ide-config.sh vscode nocturne:RO hatch:RW ide-config.sh vscode all # implicitly :RW ide-config.sh eclipse nocturne_fp > ~/Downloads/nocturne_fp-RW.xml HEREDOC } # Usage: iprintf [printf-args...] iprintf() { local level=$1 shift local n=$((INDENT_WIDTH*level)) if [[ $n -ne 0 ]]; then eval printf '"${INDENT_CHAR}%.0s"' "{1..$n}" fi # shellcheck disable=SC2059 printf "$@" return $? } # Usage: parse-cfg-board # # Example: parse-cfg-board nocturne:RW parse-cfg-board() { local cfg=$1 # Remove possible :RW or :RO local board=${cfg%%:*} if [[ -z ${board} ]]; then return 1 fi echo "${board}" } # Usage: parse-cfg-image # # Example: parse-cfg-image nocturne:RW # Example: parse-cfg-image nocturne parse-cfg-image() { local cfg=$1 local board if ! board=$(parse-cfg-board "${cfg}"); then return 1 fi # Remove known board part cfg=${cfg#${board}} cfg=${cfg#":"} # Use default image if none set cfg=${cfg:-${DEFAULT_IMAGE}} case ${cfg} in RW|RO) echo "${cfg}" return 0 ;; *) return 1 ;; esac } # Usage: make-defines make-defines() { local board=$1 local image=$2 local cache="${MAKE_CACHE_DIR}/defines/${board}-${image}" if [[ ! -f "${cache}" ]]; then make print-defines BOARD="${board}" BLD="${image}" >"${cache}" fi cat "${cache}" } # Usage: make-includes # # Rerun a newline separated list of include directories relative to the ec's # root directory. make-includes() { local board=$1 local image=$2 local cache="${MAKE_CACHE_DIR}/includes/${board}-${image}" if [[ ! -f "${cache}" ]]; then make print-includes BOARD="${board}" BLD="${image}" \ | xargs realpath --relative-to=. \ | { if [[ "${INCLUDES_DROP_ROOT}" == true ]]; then grep -v "^\.$" else cat fi } >"${cache}" fi cat "${cache}" } # Usage: make-boards make-boards() { local cache="${MAKE_CACHE_DIR}/boards" if [[ ! -f "${cache}" ]]; then make print-boards >"${cache}" fi cat "${cache}" } # Usage: | join # # JSON: includes nocturne_fp RW | join '"' '"' ',\n' # C: includes nocturne_fp RW | join '"' '",' '\n' join() { local left=$1 local right=$2 local sep=$3 local first=true while read -r line; do # JSON is ridiculous for not allowing a trailing , if [[ "${first}" == true ]]; then first=false else printf "%b" "${sep}" fi printf "%b%s%b" "${left}" "${line}" "${right}" done echo } # Usage: | encap