Makefile (14907B)
1 # 2 # DON'T CONFIGURE THE BUILD HERE! USE config.mk INSTEAD! 3 # 4 # Generic Makefile that: 5 # - Autodetects C/C++ compilers to use, allowing to set defaults via cli params; 6 # - Autodetects all source files in the tree, compiling each one to a obj and 7 # linking everything together in a final executable. 8 # 9 # Written by Gabriel de Brito (@gboncoffee everywhere) designed specifically for 10 # the use cases of the HiPES research group (https://web.inf.ufpr.br/hipes/). 11 # 12 # This software is dual-licensed under Public Domain (CC0) or MIT. Choose what 13 # fits best for you. Licenses text below. 14 # 15 # Contact mails: 16 # - me@gboncoffee.dev.br (probably still exists). 17 # - ggb23@inf.ufpr.br (prefer this as it is my academic mail, but it may not 18 # exist anymore if you're reading far enough in the future). 19 # 20 # TODO: 21 # - Assembly files support; 22 # - Better compiler autodection technology (via a configurable list maybe?); 23 # - Autodetection of headers associated with a source file. 24 # 25 26 # 27 # How to use and configure this build system: 28 # 29 # The targets that you should use are just target, debug and clean. The target 30 # target is the default and builds the "release mode". The debug builds the 31 # "debug mode", and clean removes the directory with objects and the binaries. 32 # 33 # Optionally set variables in the config.mk file. It works if that files doesn't 34 # even exist, so no worry, everything has a sane default. 35 # 36 # Variables: 37 # 38 # SRC (path): Directory with source files. Defaults to "src/". 39 # 40 # BUILD_TARGET (path): Directory with object files. Defaults to "build/". 41 # 42 # TARGET (path): Executable to create. Defaults to "a.out". 43 # 44 # DEBUG_SUFFIX (string): Will be appended to all objects and the target before 45 # extension, as to compile the versions with debug symbols. Defaults to 46 # "-debug". 47 # 48 # CPP_EXTENSION (string): File extension (without dot) to consider as C++ files, 49 # as some may use .cpp and some .cc. Defaults to "cpp". 50 # 51 # CC (path): Default C compiler to use. If this variable is set and the compiler 52 # does not exist in PATH, it autodetects between the following compilers (in 53 # this order): clang, gcc, cc. 54 # 55 # CPP (path): Same as CC but for C++. Autodetects between clang++, g++ and c++. 56 # 57 # FORCE_CC (bool): If this variable is set, refuses to try compiling with a 58 # different compiler than the default set with CC. Does nothing without CC being 59 # set. 60 # 61 # FORCE_CPP (bool): Same as FORCE_CC but for C++. 62 # 63 # CFLAGS (list): Default C compiler flags. Defaults to "-Wall -Wextra". 64 # 65 # C_DEBUG_FLAGS (list): Additional C compiler flags for debug build mode. 66 # Defaults to "-Og -g". 67 # 68 # C_RELEASE_FLAGS (list): Additional C compiler flags for release build mode. 69 # Defaults to "-O2". 70 # 71 # CPPFLAGS (list): Same as CFLAGS but for C++. Same defaults. 72 # 73 # CPP_DEBUG_FLAGS (list): Same as C_DEBUG_FLAGS but for C++. Same defaults. 74 # 75 # CPP_RELEASE_FLAGS (list): Same as C_RELEASE_FLAGS but for C++. Same defaults. 76 # 77 # LD_FLAGS (list): Flags to pass when linking. Defaults to none (unset). 78 # 79 80 # 81 # License option: CC0 (Public Domain) 82 # 83 # Creative Commons Legal Code 84 # 85 # CC0 1.0 Universal 86 # 87 # CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE 88 # LEGAL SERVICES. DISTRIBUTION OF THIS DOCUMENT DOES NOT CREATE AN 89 # ATTORNEY-CLIENT RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS 90 # INFORMATION ON AN "AS-IS" BASIS. CREATIVE COMMONS MAKES NO WARRANTIES 91 # REGARDING THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS 92 # PROVIDED HEREUNDER, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM 93 # THE USE OF THIS DOCUMENT OR THE INFORMATION OR WORKS PROVIDED 94 # HEREUNDER. 95 # 96 # Statement of Purpose 97 # 98 # The laws of most jurisdictions throughout the world automatically confer 99 # exclusive Copyright and Related Rights (defined below) upon the creator 100 # and subsequent owner(s) (each and all, an "owner") of an original work of 101 # authorship and/or a database (each, a "Work"). 102 # 103 # Certain owners wish to permanently relinquish those rights to a Work for 104 # the purpose of contributing to a commons of creative, cultural and 105 # scientific works ("Commons") that the public can reliably and without fear 106 # of later claims of infringement build upon, modify, incorporate in other 107 # works, reuse and redistribute as freely as possible in any form whatsoever 108 # and for any purposes, including without limitation commercial purposes. 109 # These owners may contribute to the Commons to promote the ideal of a free 110 # culture and the further production of creative, cultural and scientific 111 # works, or to gain reputation or greater distribution for their Work in 112 # part through the use and efforts of others. 113 # 114 # For these and/or other purposes and motivations, and without any 115 # expectation of additional consideration or compensation, the person 116 # associating CC0 with a Work (the "Affirmer"), to the extent that he or she 117 # is an owner of Copyright and Related Rights in the Work, voluntarily 118 # elects to apply CC0 to the Work and publicly distribute the Work under its 119 # terms, with knowledge of his or her Copyright and Related Rights in the 120 # Work and the meaning and intended legal effect of CC0 on those rights. 121 # 122 # 1. Copyright and Related Rights. A Work made available under CC0 may be 123 # protected by copyright and related or neighboring rights ("Copyright and 124 # Related Rights"). Copyright and Related Rights include, but are not 125 # limited to, the following: 126 # 127 # i. the right to reproduce, adapt, distribute, perform, display, 128 # communicate, and translate a Work; 129 # ii. moral rights retained by the original author(s) and/or performer(s); 130 # iii. publicity and privacy rights pertaining to a person's image or 131 # likeness depicted in a Work; 132 # iv. rights protecting against unfair competition in regards to a Work, 133 # subject to the limitations in paragraph 4(a), below; 134 # v. rights protecting the extraction, dissemination, use and reuse of data 135 # in a Work; 136 # vi. database rights (such as those arising under Directive 96/9/EC of the 137 # European Parliament and of the Council of 11 March 1996 on the legal 138 # protection of databases, and under any national implementation 139 # thereof, including any amended or successor version of such 140 # directive); and 141 # vii. other similar, equivalent or corresponding rights throughout the 142 # world based on applicable law or treaty, and any national 143 # implementations thereof. 144 # 145 # 2. Waiver. To the greatest extent permitted by, but not in contravention 146 # of, applicable law, Affirmer hereby overtly, fully, permanently, 147 # irrevocably and unconditionally waives, abandons, and surrenders all of 148 # Affirmer's Copyright and Related Rights and associated claims and causes 149 # of action, whether now known or unknown (including existing as well as 150 # future claims and causes of action), in the Work (i) in all territories 151 # worldwide, (ii) for the maximum duration provided by applicable law or 152 # treaty (including future time extensions), (iii) in any current or future 153 # medium and for any number of copies, and (iv) for any purpose whatsoever, 154 # including without limitation commercial, advertising or promotional 155 # purposes (the "Waiver"). Affirmer makes the Waiver for the benefit of each 156 # member of the public at large and to the detriment of Affirmer's heirs and 157 # successors, fully intending that such Waiver shall not be subject to 158 # revocation, rescission, cancellation, termination, or any other legal or 159 # equitable action to disrupt the quiet enjoyment of the Work by the public 160 # as contemplated by Affirmer's express Statement of Purpose. 161 # 162 # 3. Public License Fallback. Should any part of the Waiver for any reason 163 # be judged legally invalid or ineffective under applicable law, then the 164 # Waiver shall be preserved to the maximum extent permitted taking into 165 # account Affirmer's express Statement of Purpose. In addition, to the 166 # extent the Waiver is so judged Affirmer hereby grants to each affected 167 # person a royalty-free, non transferable, non sublicensable, non exclusive, 168 # irrevocable and unconditional license to exercise Affirmer's Copyright and 169 # Related Rights in the Work (i) in all territories worldwide, (ii) for the 170 # maximum duration provided by applicable law or treaty (including future 171 # time extensions), (iii) in any current or future medium and for any number 172 # of copies, and (iv) for any purpose whatsoever, including without 173 # limitation commercial, advertising or promotional purposes (the 174 # "License"). The License shall be deemed effective as of the date CC0 was 175 # applied by Affirmer to the Work. Should any part of the License for any 176 # reason be judged legally invalid or ineffective under applicable law, such 177 # partial invalidity or ineffectiveness shall not invalidate the remainder 178 # of the License, and in such case Affirmer hereby affirms that he or she 179 # will not (i) exercise any of his or her remaining Copyright and Related 180 # Rights in the Work or (ii) assert any associated claims and causes of 181 # action with respect to the Work, in either case contrary to Affirmer's 182 # express Statement of Purpose. 183 # 184 # 4. Limitations and Disclaimers. 185 # 186 # a. No trademark or patent rights held by Affirmer are waived, abandoned, 187 # surrendered, licensed or otherwise affected by this document. 188 # b. Affirmer offers the Work as-is and makes no representations or 189 # warranties of any kind concerning the Work, express, implied, 190 # statutory or otherwise, including without limitation warranties of 191 # title, merchantability, fitness for a particular purpose, non 192 # infringement, or the absence of latent or other defects, accuracy, or 193 # the present or absence of errors, whether or not discoverable, all to 194 # the greatest extent permissible under applicable law. 195 # c. Affirmer disclaims responsibility for clearing rights of other persons 196 # that may apply to the Work or any use thereof, including without 197 # limitation any person's Copyright and Related Rights in the Work. 198 # Further, Affirmer disclaims responsibility for obtaining any necessary 199 # consents, permissions or other rights required for any use of the 200 # Work. 201 # d. Affirmer understands and acknowledges that Creative Commons is not a 202 # party to this document and has no duty or obligation with respect to 203 # this CC0 or use of the Work. 204 # 205 206 # 207 # License option: MIT 208 # 209 # Copyright (C) 2024 Gabriel de Brito 210 # 211 # Permission is hereby granted, free of charge, to any person obtaining a copy 212 # of this software and associated documentation files (the "Software"), to deal 213 # in the Software without restriction, including without limitation the rights 214 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 215 # copies of the Software, and to permit persons to whom the Software is 216 # furnished to do so, subject to the following conditions: 217 # The above copyright notice and this permission notice shall be included in 218 # all copies or substantial portions of the Software. 219 # 220 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 221 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 222 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 223 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 224 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 225 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 226 # THE SOFTWARE. 227 # 228 229 undefine CC 230 undefine CPP 231 232 -include config.mk 233 234 # Build variables. 235 SRC ?= src/ 236 BUILD_TARGET ?= build/ 237 TARGET ?= a.out 238 DEBUG_SUFFIX ?= -debug 239 240 # C variables. 241 CFLAGS ?= -Wall -Wextra 242 C_DEBUG_FLAGS ?= -Og -g 243 C_RELEASE_FLAGS ?= -O2 244 245 # C++ variables. 246 CPPFLAGS ?= -Wall -Wextra 247 CPP_DEBUG_FLAGS ?= -Og -g 248 CPP_RELEASE_FLAGS ?= -O2 249 CPP_EXTENSION ?= cpp 250 251 # 252 # Compiler autodetection technology. This is particularly ugly because it looks 253 # like Richard Stallman doesn't know how elses work. 254 # 255 256 ifdef FORCE_CC 257 ifndef CC 258 $(error "Refusing to compile without CC being set due to FORCE_CC being set.") 259 else 260 ifeq "$(shell which $(CC))" "" 261 $(error "Refusing to compile without $(CC) in PATH due to FORCE_CC being set.") 262 endif 263 endif 264 else 265 ifndef CC 266 CC = clang 267 ifeq "$(shell which clang)" "" 268 CC = gcc 269 ifeq "$(shell which gcc)" "" 270 CC = cc 271 ifeq "$(shell which cc)" "" 272 $(error "Could not detect C compiler. Set one with CC or install clang, gcc or cc.") 273 endif 274 endif 275 endif 276 endif 277 endif 278 279 ifdef FORCE_CPP 280 ifndef CPP 281 $(error "Refusing to compile without CPP being set due to FORCE_CPP being set.") 282 else 283 ifeq "$(shell which $(CPP))" "" 284 $(error "Refusing to compile without $(CPP) in PATH due to FORCE_CPP being set.") 285 endif 286 endif 287 else 288 ifndef CC 289 CPP = clang++ 290 ifeq "$(shell which clang++)" "" 291 CPP = g++ 292 ifeq "$(shell which g++)" "" 293 CPP = c++ 294 ifeq "$(shell which c++)" "" 295 $(error "Could not detect C compiler. Set one with CPP or install clang++, g++ or c++.") 296 endif 297 endif 298 endif 299 endif 300 endif 301 302 # 303 # Autodetection of source files technology. 304 # 305 306 C_SRCS = $(shell find $(SRC) -type f -name "*.c") 307 C_OBJS = $(patsubst $(SRC)%.c, $(BUILD_TARGET)%.o, $(C_SRCS)) 308 C_DEBUG_OBJS = $(patsubst %.o, %$(DEBUG_SUFFIX).o, $(C_OBJS)) 309 310 CPP_SRCS = $(shell find $(SRC) -type f -name "*.$(CPP_EXTENSION)") 311 CPP_OBJS = $(patsubst $(SRC)%.$(CPP_EXTENSION), $(BUILD_TARGET)%.o, $(CPP_SRCS)) 312 CPP_DEBUG_OBJS = $(patsubst %.o, %$(DEBUG_SUFFIX).o, $(CPP_OBJS)) 313 314 # Autodetect directories that need to be created. This tries to create them 315 # every time, if someone has a better solution please tell me. 316 SRC_DIRECTORIES = $(shell find $(SRC) -type d) 317 BUILD_TARGET_DIRECTORIES = $(patsubst $(SRC)%, $(BUILD_TARGET)%, $(SRC_DIRECTORIES)) 318 319 # 320 # Detect if we're going to link with $(CC) or $(CPP) by simply checking if we're 321 # going to compile any C++ at all. 322 # 323 ifeq "$(CPP_SRCS)" "" 324 LD = $(CC) 325 else 326 LD = $(CPP) 327 endif 328 329 # 330 # Rules. 331 # 332 333 .PHONY: target debug clean 334 335 target: $(TARGET) 336 337 debug: $(TARGET)$(DEBUG_SUFFIX) 338 339 $(TARGET): $(CPP_OBJS) $(C_OBJS) | $(BUILD_TARGET_DIRECTORIES) 340 $(LD) $^ -o $@ $(LD_FLAGS) 341 342 $(BUILD_TARGET)%.o: $(SRC)%.c | $(BUILD_TARGET_DIRECTORIES) 343 $(CC) $(CFLAGS) $(C_RELEASE_FLAGS) -c $^ -o $@ 344 345 $(BUILD_TARGET)%.o: $(SRC)%.$(CPP_EXTENSION) | $(BUILD_TARGET_DIRECTORIES) 346 $(CPP) $(CPPFLAGS) $(CPP_RELEASE_FLAGS) -c $^ -o $@ 347 348 $(TARGET)$(DEBUG_SUFFIX): $(CPP_DEBUG_OBJS) $(C_DEBUG_OBJS) | $(BUILD_TARGET_DIRECTORIES) 349 $(LD) $^ -o $@ $(LD_FLAGS) 350 351 $(BUILD_TARGET)%$(DEBUG_SUFFIX).o: $(SRC)%.c | $(BUILD_TARGET_DIRECTORIES) 352 $(CC) $(CFLAGS) $(C_DEBUG_FLAGS) -c $^ -o $@ 353 354 $(BUILD_TARGET)%$(DEBUG_SUFFIX).o: $(SRC)%.$(CPP_EXTENSION) | $(BUILD_TARGET_DIRECTORIES) 355 $(CPP) $(CPPFLAGS) $(CPP_DEBUG_FLAGS) -c $^ -o $@ 356 357 $(BUILD_TARGET_DIRECTORIES): 358 -mkdir -p $@ 359 360 clean: 361 -rm -r $(BUILD_TARGET) 362 -rm $(TARGET) 363 -rm $(TARGET)$(DEBUG_SUFFIX)