Config.mk 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. ############################################################################
  2. # tools/Config.mk
  3. # Global build rules and macros.
  4. #
  5. # Copyright (C) 2011, 2013-2014, 2018-2019, 2020 Gregory Nutt. All rights
  6. # reserved.
  7. # Author: Richard Cochran
  8. # Gregory Nutt <gnutt@nuttx.org>
  9. #
  10. # This file (along with $(TOPDIR)/.config) must be included by every
  11. # configuration-specific Make.defs file.
  12. #
  13. # Redistribution and use in source and binary forms, with or without
  14. # modification, are permitted provided that the following conditions
  15. # are met:
  16. #
  17. # 1. Redistributions of source code must retain the above copyright
  18. # notice, this list of conditions and the following disclaimer.
  19. # 2. Redistributions in binary form must reproduce the above copyright
  20. # notice, this list of conditions and the following disclaimer in
  21. # the documentation and/or other materials provided with the
  22. # distribution.
  23. # 3. Neither the name NuttX nor the names of its contributors may be
  24. # used to endorse or promote products derived from this software
  25. # without specific prior written permission.
  26. #
  27. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  28. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  29. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  30. # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  31. # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  32. # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  33. # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  34. # OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  35. # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. # POSSIBILITY OF SUCH DAMAGE.
  39. #
  40. ############################################################################
  41. # Disable all built-in rules
  42. .SUFFIXES:
  43. ifeq ($(CONFIG_WINDOWS_NATIVE),y)
  44. export SHELL=cmd
  45. endif
  46. # Control build verbosity
  47. #
  48. # V=1,2: Enable echo of commands
  49. # V=2: Enable bug/verbose options in tools and scripts
  50. ifeq ($(V),1)
  51. export Q :=
  52. else ifeq ($(V),2)
  53. export Q :=
  54. else
  55. export Q := @
  56. endif
  57. # These are configuration variables that are quoted by configuration tool
  58. # but which must be unquoted when used in the build system.
  59. CONFIG_ARCH := $(patsubst "%",%,$(strip $(CONFIG_ARCH)))
  60. CONFIG_ARCH_CHIP := $(patsubst "%",%,$(strip $(CONFIG_ARCH_CHIP)))
  61. CONFIG_ARCH_BOARD := $(patsubst "%",%,$(strip $(CONFIG_ARCH_BOARD)))
  62. # Some defaults.
  63. # $(TOPDIR)/Make.defs can override these appropriately.
  64. MODULECC ?= $(CC)
  65. MODULELD ?= $(LD)
  66. MODULESTRIP ?= $(STRIP)
  67. # Define HOSTCC on the make command line if it differs from these defaults
  68. # Define HOSTCFLAGS with -g on the make command line to build debug versions
  69. HOSTOS = ${shell uname -o 2>/dev/null || uname -s 2>/dev/null || echo "Other"}
  70. ifeq ($(HOSTOS),MinGW)
  71. # In the Windows native environment, the MinGW GCC compiler is used
  72. HOSTCC ?= mingw32-gcc.exe
  73. HOSTCFLAGS ?= -O2 -Wall -Wstrict-prototypes -Wshadow -DCONFIG_WINDOWS_NATIVE=y
  74. else
  75. # GCC or clang is assumed in all other POSIX environments
  76. # (Linux, Cygwin, MSYS2, macOS).
  77. # strtok_r is used in some tools, but does not seem to be available in
  78. # the MinGW environment.
  79. HOSTCC ?= cc
  80. HOSTCFLAGS ?= -O2 -Wall -Wstrict-prototypes -Wshadow
  81. HOSTCFLAGS += -DHAVE_STRTOK_C=1
  82. ifeq ($(HOSTOS),Cygwin)
  83. HOSTCFLAGS += -DHOST_CYGWIN=1
  84. endif
  85. endif
  86. # Some defaults just to prohibit some bad behavior if for some reason they
  87. # are not defined
  88. ASMEXT ?= .S
  89. OBJEXT ?= .o
  90. LIBEXT ?= .a
  91. ifeq ($(HOSTOS),Cygwin)
  92. EXEEXT ?= .exe
  93. endif
  94. ifeq ($(CONFIG_HOST_WINDOWS),y)
  95. HOSTEXEEXT ?= .exe
  96. endif
  97. # This define is passed as EXTRAFLAGS for kernel-mode builds. It is also passed
  98. # during PASS1 (but not PASS2) context and depend targets.
  99. KDEFINE ?= ${shell $(DEFINE) "$(CC)" __KERNEL__}
  100. # DELIM - Path segment delimiter character
  101. #
  102. # Depends on this settings defined in board-specific defconfig file installed
  103. # at $(TOPDIR)/.config:
  104. #
  105. # CONFIG_WINDOWS_NATIVE - Defined for a Windows native build
  106. ifeq ($(CONFIG_WINDOWS_NATIVE),y)
  107. DELIM ?= $(strip \)
  108. else
  109. DELIM ?= $(strip /)
  110. endif
  111. # Process board-specific directories
  112. ifeq ($(CONFIG_ARCH_BOARD_CUSTOM),y)
  113. CUSTOM_DIR = $(patsubst "%",%,$(CONFIG_ARCH_BOARD_CUSTOM_DIR))
  114. ifeq ($(CONFIG_ARCH_BOARD_CUSTOM_DIR_RELPATH),y)
  115. BOARD_DIR ?= $(TOPDIR)$(DELIM)$(CUSTOM_DIR)
  116. else
  117. BOARD_DIR ?= $(CUSTOM_DIR)
  118. endif
  119. else
  120. BOARD_DIR ?= $(TOPDIR)$(DELIM)boards$(DELIM)$(CONFIG_ARCH)$(DELIM)$(CONFIG_ARCH_CHIP)$(DELIM)$(CONFIG_ARCH_BOARD)
  121. endif
  122. BOARD_COMMON_DIR ?= $(wildcard $(BOARD_DIR)$(DELIM)..$(DELIM)common)
  123. BOARD_DRIVERS_DIR ?= $(wildcard $(BOARD_DIR)$(DELIM)..$(DELIM)drivers)
  124. ifeq ($(BOARD_DRIVERS_DIR),)
  125. BOARD_DRIVERS_DIR = $(TOPDIR)$(DELIM)drivers$(DELIM)dummy
  126. endif
  127. # DIRLINK - Create a directory link in the portable way
  128. ifeq ($(CONFIG_WINDOWS_NATIVE),y)
  129. ifeq ($(CONFIG_WINDOWS_MKLINK),y)
  130. DIRLINK ?= $(TOPDIR)$(DELIM)tools$(DELIM)link.bat
  131. else
  132. DIRLINK ?= $(TOPDIR)$(DELIM)tools$(DELIM)copydir.bat
  133. endif
  134. DIRUNLINK ?= $(TOPDIR)$(DELIM)tools$(DELIM)unlink.bat
  135. else
  136. ifeq ($(CONFIG_CYGWIN_WINTOOL),y)
  137. DIRLINK ?= $(TOPDIR)$(DELIM)tools$(DELIM)copydir.sh
  138. else ifeq ($(CONFIG_WINDOWS_MSYS),y)
  139. DIRLINK ?= $(TOPDIR)$(DELIM)tools$(DELIM)copydir.sh
  140. else
  141. DIRLINK ?= $(TOPDIR)$(DELIM)tools$(DELIM)link.sh
  142. endif
  143. DIRUNLINK ?= $(TOPDIR)$(DELIM)tools$(DELIM)unlink.sh
  144. endif
  145. # MKDEP - Create the depend rule in the portable way
  146. ifeq ($(CONFIG_WINDOWS_NATIVE),y)
  147. MKDEP ?= $(TOPDIR)$(DELIM)tools$(DELIM)mkdeps$(HOSTEXEEXT) --winnative
  148. else ifeq ($(CONFIG_CYGWIN_WINTOOL),y)
  149. MKDEP ?= $(TOPDIR)$(DELIM)tools$(DELIM)mkwindeps.sh
  150. else
  151. MKDEP ?= $(TOPDIR)$(DELIM)tools$(DELIM)mkdeps$(HOSTEXEEXT)
  152. endif
  153. # INCDIR - Convert a list of directory paths to a list of compiler include
  154. # directories
  155. # Example: CFFLAGS += ${shell $(INCDIR) [options] "compiler" "dir1" "dir2" "dir2" ...}
  156. #
  157. # Note that the compiler string and each directory path string must quoted if
  158. # they contain spaces or any other characters that might get mangled by the
  159. # shell
  160. #
  161. # Depends on this setting passed as a make command line definition from the
  162. # toplevel Makefile:
  163. #
  164. # TOPDIR - The path to the top level NuttX directory in the form
  165. # appropriate for the current build environment
  166. #
  167. # Depends on this settings defined in board-specific defconfig file installed
  168. # at $(TOPDIR)/.config:
  169. #
  170. # CONFIG_WINDOWS_NATIVE - Defined for a Windows native build
  171. ifeq ($(CONFIG_WINDOWS_NATIVE),y)
  172. DEFINE ?= "$(TOPDIR)\tools\define.bat"
  173. INCDIR ?= "$(TOPDIR)\tools\incdir.bat"
  174. else ifeq ($(CONFIG_CYGWIN_WINTOOL),y)
  175. DEFINE ?= "$(TOPDIR)/tools/define.sh" -w
  176. INCDIR ?= "$(TOPDIR)/tools/incdir$(HOSTEXEEXT)" -w
  177. else
  178. DEFINE ?= "$(TOPDIR)/tools/define.sh"
  179. INCDIR ?= "$(TOPDIR)/tools/incdir$(HOSTEXEEXT)"
  180. endif
  181. # PREPROCESS - Default macro to run the C pre-processor
  182. # Example: $(call PREPROCESS, in-file, out-file)
  183. #
  184. # Depends on these settings defined in board-specific Make.defs file
  185. # installed at $(TOPDIR)/Make.defs:
  186. #
  187. # CPP - The command to invoke the C pre-processor
  188. # CPPFLAGS - Options to pass to the C pre-processor
  189. #
  190. # '<filename>.c_CPPFLAGS += <options>' may also be used, as an example, to
  191. # change the options used with the single file <filename>.c (or
  192. # <filename>.S)
  193. define PREPROCESS
  194. @echo "CPP: $1->$2"
  195. $(Q) $(CPP) $(CPPFLAGS) $($(strip $1)_CPPFLAGS) $1 -o $2
  196. endef
  197. # COMPILE - Default macro to compile one C file
  198. # Example: $(call COMPILE, in-file, out-file)
  199. #
  200. # Depends on these settings defined in board-specific Make.defs file
  201. # installed at $(TOPDIR)/Make.defs:
  202. #
  203. # CC - The command to invoke the C compiler
  204. # CFLAGS - Options to pass to the C compiler
  205. #
  206. # '<filename>.c_CFLAGS += <options>' may also be used, as an example, to
  207. # change the options used with the single file <filename>.c
  208. define COMPILE
  209. @echo "CC: $1"
  210. $(Q) $(CC) -c $(CFLAGS) $($(strip $1)_CFLAGS) $1 -o $2
  211. endef
  212. # COMPILEXX - Default macro to compile one C++ file
  213. # Example: $(call COMPILEXX, in-file, out-file)
  214. #
  215. # Depends on these settings defined in board-specific Make.defs file
  216. # installed at $(TOPDIR)/Make.defs:
  217. #
  218. # CXX - The command to invoke the C++ compiler
  219. # CXXFLAGS - Options to pass to the C++ compiler
  220. #
  221. # '<filename>.cxx_CXXFLAGS += <options>' may also be used, as an example, to
  222. # change the options used with the single file <filename>.cxx. The
  223. # extension .cpp could also be used. The same applies mutatis mutandis.
  224. define COMPILEXX
  225. @echo "CXX: $1"
  226. $(Q) $(CXX) -c $(CXXFLAGS) $($(strip $1)_CXXFLAGS) $1 -o $2
  227. endef
  228. # ASSEMBLE - Default macro to assemble one assembly language file
  229. # Example: $(call ASSEMBLE, in-file, out-file)
  230. #
  231. # NOTE that the most common toolchain, GCC, uses the compiler to assemble
  232. # files because this has the advantage of running the C Pre-Processor against
  233. # the assembly language files. This is not possible with other toolchains;
  234. # platforms using those other tools should define AS and over-ride this
  235. # definition in order to use the assembler directly.
  236. #
  237. # Depends on these settings defined in board-specific Make.defs file
  238. # installed at $(TOPDIR)/Make.defs:
  239. #
  240. # CC - By default, the C compiler is used to compile assembly language
  241. # files
  242. # AFLAGS - Options to pass to the C+compiler
  243. #
  244. # '<filename>.s_AFLAGS += <options>' may also be used, as an example, to change
  245. # the options used with the single file <filename>.s. The extension .asm
  246. # is used by some toolchains. The same applies mutatis mutandis.
  247. define ASSEMBLE
  248. @echo "AS: $1"
  249. $(Q) $(CC) -c $(AFLAGS) $1 $($(strip $1)_AFLAGS) -o $2
  250. endef
  251. # INSTALL_LIB - Install a library $1 into target $2
  252. # Example: $(call INSTALL_LIB, libabc.a, $(TOPDIR)/staging/)
  253. define INSTALL_LIB
  254. @echo "IN: $1 -> $2"
  255. $(Q) install -m 0644 $1 $2
  256. endef
  257. # ARCHIVE - Add a list of files to an archive
  258. # Example: $(call ARCHIVE, archive-file, "file1 file2 file3 ...")
  259. #
  260. # Note: The fileN strings may not contain spaces or characters that may be
  261. # interpreted strangely by the shell
  262. #
  263. # Depends on these settings defined in board-specific Make.defs file
  264. # installed at $(TOPDIR)/Make.defs:
  265. #
  266. # AR - The command to invoke the archiver (includes any options)
  267. #
  268. # Depends on this settings defined in board-specific defconfig file installed
  269. # at $(TOPDIR)/.config:
  270. #
  271. # CONFIG_WINDOWS_NATIVE - Defined for a Windows native build
  272. define ARCHIVE
  273. $(AR) $1 $(2)
  274. endef
  275. # PRELINK - Prelink a list of files
  276. # This is useful when files were compiled with fvisibility=hidden.
  277. # Any symbol which was not explicitly made global is invisible outside the
  278. # prelinked file.
  279. #
  280. # Example: $(call PRELINK, prelink-file, "file1 file2 file3 ...")
  281. #
  282. # Note: The fileN strings may not contain spaces or characters that may be
  283. # interpreted strangely by the shell
  284. #
  285. # Depends on these settings defined in board-specific Make.defs file
  286. # installed at $(TOPDIR)/Make.defs:
  287. #
  288. # LD - The command to invoke the linker (includes any options)
  289. # OBJCOPY - The command to invoke the object cop (includes any options)
  290. #
  291. # Depends on this settings defined in board-specific defconfig file installed
  292. # at $(TOPDIR)/.config:
  293. #
  294. # CONFIG_WINDOWS_NATIVE - Defined for a Windows native build
  295. ifeq ($(CONFIG_WINDOWS_NATIVE),y)
  296. define PRELINK
  297. @echo PRELINK: $1
  298. $(Q) $(LD) -Ur -o $1 $2 && $(OBJCOPY) --localize-hidden $1
  299. endef
  300. else
  301. define PRELINK
  302. @echo "PRELINK: $1"
  303. $(Q) $(LD) -Ur -o $1 $2 && $(OBJCOPY) --localize-hidden $1
  304. endef
  305. endif
  306. # POSTBUILD -- Perform post build operations
  307. # Some architectures require the use of special tools and special handling
  308. # AFTER building the NuttX binary. Make.defs files for thos architectures
  309. # should override the following define with the correct operations for
  310. # that platform
  311. define POSTBUILD
  312. endef
  313. # DELFILE - Delete one file
  314. ifeq ($(CONFIG_WINDOWS_NATIVE),y)
  315. define DELFILE
  316. $(Q) if exist $1 (del /f /q $1)
  317. endef
  318. else
  319. define DELFILE
  320. $(Q) rm -f $1
  321. endef
  322. endif
  323. # DELDIR - Delete one directory
  324. ifeq ($(CONFIG_WINDOWS_NATIVE),y)
  325. define DELDIR
  326. $(Q) if exist $1 (rmdir /q /s $1)
  327. endef
  328. else
  329. define DELDIR
  330. $(Q) rm -rf $1
  331. endef
  332. endif
  333. # MOVEFILE - Move one file
  334. ifeq ($(CONFIG_WINDOWS_NATIVE),y)
  335. define MOVEFILE
  336. $(Q) if exist $1 (move /Y $1 $2)
  337. endef
  338. else
  339. define MOVEFILE
  340. $(Q) mv -f $1 $2
  341. endef
  342. endif
  343. # COPYFILE - Copy one file
  344. ifeq ($(CONFIG_WINDOWS_NATIVE),y)
  345. define COPYFILE
  346. $(Q) if exist $1 (copy /y /b $1 $2)
  347. endef
  348. else
  349. define COPYFILE
  350. $(Q) cp -f $1 $2
  351. endef
  352. endif
  353. # CATFILE - Cat a list of files
  354. #
  355. # USAGE: $(call CATFILE,dest,src1,src2,src3,...)
  356. ifeq ($(CONFIG_WINDOWS_NATIVE),y)
  357. define CATFILE
  358. $(Q) type $(2) > $1
  359. endef
  360. else
  361. define CATFILE
  362. $(Q) cat $(2) > $1
  363. endef
  364. endif
  365. # RWILDCARD - Recursive wildcard used to get lists of files from directories
  366. #
  367. # USAGE: FILELIST = $(call RWILDCARD,<dir>,<wildcard-filename)
  368. #
  369. # This is functionally equivalent to the following, but has the advantage in
  370. # that it is portable
  371. #
  372. # FILELIST = ${shell find <dir> -name <wildcard-file>}
  373. define RWILDCARD
  374. $(foreach d,$(wildcard $1/*),$(call RWILDCARD,$d,$2)$(filter $(subst *,%,$2),$d))
  375. endef
  376. # CLEAN - Default clean target
  377. ifeq ($(CONFIG_WINDOWS_NATIVE),y)
  378. define CLEAN
  379. $(Q) if exist *$(OBJEXT) (del /f /q *$(OBJEXT))
  380. $(Q) if exist *$(LIBEXT) (del /f /q *$(LIBEXT))
  381. $(Q) if exist *~ (del /f /q *~)
  382. $(Q) if exist (del /f /q .*.swp)
  383. endef
  384. else
  385. define CLEAN
  386. $(Q) rm -f *$(OBJEXT) *$(LIBEXT) *~ .*.swp
  387. endef
  388. endif
  389. # TESTANDREPLACEFILE - Test if two files are different. If so replace the
  390. # second with the first. Otherwise, delete the first.
  391. #
  392. # USAGE: $(call TESTANDREPLACEFILE, newfile, oldfile)
  393. #
  394. # args: $1 - newfile: Temporary file to test
  395. # $2 - oldfile: File to replace
  396. ifeq ($(CONFIG_WINDOWS_NATIVE),y)
  397. define TESTANDREPLACEFILE
  398. $(Q) move /Y $1 $2
  399. endef
  400. else
  401. define TESTANDREPLACEFILE
  402. if [ -f $2 ]; then \
  403. if cmp $1 $2; then \
  404. rm -f $1; \
  405. else \
  406. mv $1 $2; \
  407. fi \
  408. else \
  409. mv $1 $2; \
  410. fi
  411. endef
  412. endif
  413. # Invoke make
  414. define MAKE_template
  415. +$(Q) $(MAKE) -C $(1) $(2) TOPDIR="$(TOPDIR)" APPDIR="$(APPDIR)"
  416. endef
  417. define SDIR_template
  418. $(1)_$(2):
  419. +$(Q) $(MAKE) -C $(1) $(2) TOPDIR="$(TOPDIR)" APPDIR="$(APPDIR)"
  420. endef
  421. # The default C/C++ search path
  422. ARCHINCLUDES += ${shell $(INCDIR) -s "$(CC)" $(TOPDIR)$(DELIM)include}
  423. ifeq ($(CONFIG_LIBCXX),y)
  424. ARCHXXINCLUDES += ${shell $(INCDIR) -s "$(CC)" $(TOPDIR)$(DELIM)include$(DELIM)libcxx}
  425. else ifeq ($(CONFIG_UCLIBCXX),y)
  426. ARCHXXINCLUDES += ${shell $(INCDIR) -s "$(CC)" $(TOPDIR)$(DELIM)include$(DELIM)uClibc++}
  427. else
  428. ARCHXXINCLUDES += ${shell $(INCDIR) -s "$(CC)" $(TOPDIR)$(DELIM)include$(DELIM)cxx}
  429. endif
  430. ARCHXXINCLUDES += ${shell $(INCDIR) -s "$(CC)" $(TOPDIR)$(DELIM)include}