|
| 1 | +############################################################################ |
| 2 | +# apps/config.nims |
| 3 | +# |
| 4 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 5 | +# contributor license agreements. See the NOTICE file distributed with |
| 6 | +# this work for additional information regarding copyright ownership. The |
| 7 | +# ASF licenses this file to you under the Apache License, Version 2.0 (the |
| 8 | +# "License"); you may not use this file except in compliance with the |
| 9 | +# License. You may obtain a copy of the License at |
| 10 | +# |
| 11 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 12 | +# |
| 13 | +# Unless required by applicable law or agreed to in writing, software |
| 14 | +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 15 | +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 16 | +# License for the specific language governing permissions and limitations |
| 17 | +# under the License. |
| 18 | +# |
| 19 | +############################################################################ |
| 20 | + |
| 21 | +import std/os |
| 22 | +import std/strutils |
| 23 | + |
| 24 | +switch "os", "nuttx" |
| 25 | +switch "mm", "orc" |
| 26 | + |
| 27 | +switch "arm.nuttx.gcc.exe", "arm-none-eabi-gcc" |
| 28 | +switch "arm64.nuttx.gcc.exe", "aarch64-none-elf-gcc" |
| 29 | +switch "riscv32.nuttx.gcc.exe", "riscv64-unknown-elf-gcc" |
| 30 | +switch "amd64.nuttx.gcc.exe", "x86_64-linux-gnu-gcc" |
| 31 | + |
| 32 | +switch "nimcache", ".nimcache" |
| 33 | +switch "d", "useStdLib" |
| 34 | +switch "d", "useMalloc" |
| 35 | +switch "d", "nimAllocPagesViaMalloc" |
| 36 | +switch "d", "noSignalHandler" |
| 37 | +switch "threads", "off" |
| 38 | +switch "noMain", "on" |
| 39 | +switch "compileOnly", "on" |
| 40 | +switch "noLinking", "on" |
| 41 | +# TODO: need OpenSSL-mbedTLS wrapper library. |
| 42 | +#switch "d", "ssl" |
| 43 | +#swich "dynlibOverride", "ssl" |
| 44 | + |
| 45 | +type |
| 46 | + OptFlag = enum |
| 47 | + oNone |
| 48 | + oSize |
| 49 | + DotConfig = object |
| 50 | + arch: string |
| 51 | + opt: OptFlag |
| 52 | + debugSymbols: bool |
| 53 | + ramSize: int |
| 54 | + isSim: bool |
| 55 | + |
| 56 | +proc killoBytes(bytes: int): int = |
| 57 | + result = (bytes / 1024).int |
| 58 | + |
| 59 | +proc read_config(cfg: string): DotConfig = |
| 60 | + for line in cfg.readFile.splitLines: |
| 61 | + if not line.startsWith("CONFIG_"): |
| 62 | + continue |
| 63 | + let keyval = line.replace("CONFIG_", "").split("=") |
| 64 | + if keyval.len != 2: |
| 65 | + continue |
| 66 | + case keyval[0] |
| 67 | + of "ARCH": |
| 68 | + let arch = keyval[1].strip(chars = {'"'}) |
| 69 | + case arch |
| 70 | + of "arm", "arm64": |
| 71 | + result.arch = arch |
| 72 | + of "riscv": |
| 73 | + result.arch = "riscv32" |
| 74 | + of "sim": |
| 75 | + if defined(amd64): |
| 76 | + result.arch = "amd64" |
| 77 | + elif defined(aarch64): |
| 78 | + result.arch = "arm64" |
| 79 | + result.isSim = true |
| 80 | + of "DEBUG_NOOPT": |
| 81 | + result.opt = oNone |
| 82 | + of "DEBUG_FULLOPT": |
| 83 | + result.opt = oSize |
| 84 | + of "DEBUG_SYMBOLS": |
| 85 | + result.debugSymbols = true |
| 86 | + of "RAM_SIZE": |
| 87 | + result.ramSize = keyval[1].parseInt |
| 88 | + echo "* arch: " & result.arch |
| 89 | + echo "* opt: " & $result.opt |
| 90 | + echo "* debug: " & $result.debugSymbols |
| 91 | + echo "* ramSize: " & $result.ramSize |
| 92 | + |
| 93 | +func bool2onoff(b: bool): string = |
| 94 | + result = if b: "on" else: "off" |
| 95 | + |
| 96 | +proc setup_cfg(cfg: DotConfig) = |
| 97 | + switch("cpu", cfg.arch) |
| 98 | + if cfg.opt == oSize: |
| 99 | + switch("define", "release") |
| 100 | + switch("define", "danger") |
| 101 | + switch("opt", "size") |
| 102 | + if cfg.debugSymbols: |
| 103 | + # use native debugger (gdb) |
| 104 | + switch("debugger", "native") |
| 105 | + let debug_onoff = cfg.debugSymbols.bool2onoff |
| 106 | + switch("lineDir", debug_onoff) |
| 107 | + switch("stackTrace", debug_onoff) |
| 108 | + switch("lineTrace", debug_onoff) |
| 109 | + if not cfg.isSim: |
| 110 | + # Adjust the page size for Nim's GC allocator. |
| 111 | + let ramKilloBytes = cfg.ramSize.killoBytes |
| 112 | + if ramKilloBytes < 32: |
| 113 | + switch("define", "nimPage256") |
| 114 | + elif ramKilloBytes < 512: |
| 115 | + switch("define", "nimPage512") |
| 116 | + elif ramKilloBytes < 2048: |
| 117 | + switch("define", "nimPage1k") |
| 118 | + if ramKilloBytes < 512: |
| 119 | + # Sets MemAlign to 4 bytes which reduces the memory alignment |
| 120 | + # to better match some embedded devices. |
| 121 | + switch("define", "nimMemAlignTiny") |
| 122 | + |
| 123 | + |
| 124 | +let topdir = getEnv("TOPDIR") |
| 125 | +let cfg = read_config(topdir & "/.config") |
| 126 | +cfg.setup_cfg() |
0 commit comments