1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
|
#!/usr/bin/awk -f
# Copyright (c) 2015-2018, Yannick Cote <yhcote@gmail.com>. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be found
# in the LICENSE file.
function usage()
{
print "usage: genmod mconflist=<module file> makeitgendir=<temp confdir>"
print " host=<host type> tmpldir=<template location>"
exit(1)
}
# remove extra spaces from a string
function trim(str)
{
gsub(/ +/, " ", str)
gsub(/ +$/, "", str)
gsub(/^ +/, "", str)
return str
}
# print all keyword vars and their values for all project modules
function printmvars()
{
reset_file("/tmp/mvars")
for (m in mconfs) {
for (k in keywords) {
if (mvars[mconfs[m], keywords[k]] == "")
continue
printf("%s:%s [%s]\n", mconfs[m], keywords[k],
mvars[mconfs[m], keywords[k]]) >> "/tmp/mvars"
}
print "" >> "/tmp/mvars"
}
}
# truncate file
function reset_file(file)
{
printf("") > file
}
# check if we are still reading keyword values or reached a new keyword
function getkeyword()
{
iskey = 0
if (words[1] != "") {
for (k in keywords) {
if (words[1] == keywords[k])
iskey = 1
}
if (iskey == 1) {
currkeywd = words[1]
} else {
print "error:", words[1], "is not a keyword"
exit(1)
}
}
}
# for a keyword (name, src, cflags, etc.) read its values
function getvalues(mconf, nfields)
{
for (j = 2; j <= nfields; j++) {
mvars[mconf, currkeywd] = mvars[mconf, currkeywd] " " words[j]
}
mvars[mconf, currkeywd] = trim(mvars[mconf, currkeywd])
}
# this routine reads and parses all mconf variables from one "mconf" file
function scanmconf(mconf, makeitgendir)
{
m = makeitgendir "/" mconf ".parsed"
while (getline < m > 0) {
n = split($0, words, " *:= *| *\\ *|[ \t]*")
if (n > 0) {
getkeyword()
getvalues(mconf, n)
}
}
}
# generate object list from [a,c]src,win_[a,c]src,unix_[a,c]src for each module
function genobjs(mconf)
{
# first "[a,c]obj"
split(mvars[mconf, "csrc"], objs, " ")
for (o in objs) {
gsub(/\.c$/, ".o", objs[o])
mvars[mconf, "cobj"] = mvars[mconf, "cobj"] "$(BUILDDIR)/" objs[o] " "
mvars[mconf, "cleanfiles"] = mvars[mconf, "cleanfiles"] " " "$(BUILDDIR)/" objs[o]
mvars[mconf, "cleanfiles"] = mvars[mconf, "cleanfiles"] " " "$(BUILDDIR)/" objs[o] ".d"
}
split(mvars[mconf, "asrc"], objs, " ")
for (o in objs) {
gsub(/\.S$/, ".o", objs[o])
mvars[mconf, "aobj"] = mvars[mconf, "aobj"] "$(BUILDDIR)/" objs[o] " "
mvars[mconf, "cleanfiles"] = mvars[mconf, "cleanfiles"] " " "$(BUILDDIR)/" objs[o]
mvars[mconf, "cleanfiles"] = mvars[mconf, "cleanfiles"] " " "$(BUILDDIR)/" objs[o] ".d"
}
# then "unix_[a,c]obj"
split(mvars[mconf, "unix_csrc"], objs, " ")
for (o in objs) {
gsub(/\.c$/, ".o", objs[o])
mvars[mconf, "unix_cobj"] = mvars[mconf, "unix_cobj"] "$(BUILDDIR)/" objs[o] " "
mvars[mconf, "cleanfiles"] = mvars[mconf, "cleanfiles"] " " "$(BUILDDIR)/" objs[o]
mvars[mconf, "cleanfiles"] = mvars[mconf, "cleanfiles"] " " "$(BUILDDIR)/" objs[o] ".d"
}
split(mvars[mconf, "unix_asrc"], objs, " ")
for (o in objs) {
gsub(/\.S$/, ".o", objs[o])
mvars[mconf, "unix_aobj"] = mvars[mconf, "unix_aobj"] "$(BUILDDIR)/" objs[o] " "
mvars[mconf, "cleanfiles"] = mvars[mconf, "cleanfiles"] " " "$(BUILDDIR)/" objs[o]
mvars[mconf, "cleanfiles"] = mvars[mconf, "cleanfiles"] " " "$(BUILDDIR)/" objs[o] ".d"
}
# then "win_[a,c]obj"
split(mvars[mconf, "win_csrc"], objs, " ")
for (o in objs) {
gsub(/\.c$/, ".o", objs[o])
mvars[mconf, "win_cobj"] = mvars[mconf, "win_cobj"] "$(BUILDDIR)/" objs[o] " "
mvars[mconf, "cleanfiles"] = mvars[mconf, "cleanfiles"] " " "$(BUILDDIR)/" objs[o]
mvars[mconf, "cleanfiles"] = mvars[mconf, "cleanfiles"] " " "$(BUILDDIR)/" objs[o] ".d"
}
split(mvars[mconf, "win_asrc"], objs, " ")
for (o in objs) {
gsub(/\.S$/, ".o", objs[o])
mvars[mconf, "win_aobj"] = mvars[mconf, "win_aobj"] "$(BUILDDIR)/" objs[o] " "
mvars[mconf, "cleanfiles"] = mvars[mconf, "cleanfiles"] " " "$(BUILDDIR)/" objs[o]
mvars[mconf, "cleanfiles"] = mvars[mconf, "cleanfiles"] " " "$(BUILDDIR)/" objs[o] ".d"
}
# finally "data"
split(mvars[mconf, "data"], objs, " ")
for (o in objs) {
objs[o] = objs[o] ".bin"
mvars[mconf, "dobj"] = mvars[mconf, "dobj"] objs[o] " "
mvars[mconf, "cleanfiles"] = mvars[mconf, "cleanfiles"] " " "$(BUILDDIR)/" objs[o]
}
}
# create the "target" mconf variable based on kind (prog, lib, obj list)
function gentarget(mconf)
{
if (mvars[mconf, "prog"] != "") {
# generate target for a program
mvars[mconf, "target"] = mvars[mconf, "prog"]
if (envar["host"] == "windows")
mvars[mconf, "target"] = mvars[mconf, "target"] ".exe"
} else if (mvars[mconf, "lib"] != "") {
# generate target for a library
mvars[mconf, "target"] = "lib" mvars[mconf, "lib"]
} else if (mvars[mconf, "data"] != "") {
# generate target for embedded data
mvars[mconf, "target"] = mvars[mconf, "name"] ".bin.o"
} else {
# generate target for a simple list of objects
mvars[mconf, "target"] = mvars[mconf, "name"] "_OBJ"
}
}
# create a list of "deps_link" list based on other mconf target this mconf needs to link with
function gendeps_link(mconf, idx)
{
# dependency is a program nothing to link with
if (mvars[mconfs[mconf], "prog"] != "")
return
# dependency is a lib, generate library link rules
if (mvars[mconfs[mconf], "lib"] != "") {
mvars[mconfs[idx], "deps_link"] = mvars[mconfs[idx], "deps_link"] " " \
"-L$(BUILDDIR)/" mconfsdirs[mconf] " -l" mvars[mconfs[mconf], "lib"]
} else if (mvars[mconfs[mconf], "data"] != "") {
mvars[mconfs[idx], "extralibs"] = "$(" mvars[mconfs[mconf], "target"] ")" " " \
mvars[mconfs[idx], "extralibs"]
} else {
# dependency is just an object list
mvars[mconfs[idx], "deps_link"] = "$(" mvars[mconfs[mconf], "target"] ")" " " \
mvars[mconfs[idx], "deps_link"]
}
}
# create a "deps_target" list based on other mconf targets this mconf depends on
function gendeps(idx)
{
split(mvars[mconfs[idx], "depends"], deps, " ")
for (d in deps) {
found = 0
for (m in mconfs) {
if (mvars[mconfs[m], "name"] == deps[d]) {
gendeps_link(m, idx)
mvars[mconfs[idx], "deps_target"] = mvars[mconfs[idx], "deps_target"] " " \
"$(" mvars[mconfs[m], "target"] ")"
found = 1
}
}
# if dependency is NOT a module name but just a verbatim expression to paste in place
if (found == 0) {
mvars[mconfs[idx], "deps_target"] = mvars[mconfs[idx], "deps_target"] " " deps[d]
}
}
mvars[mconfs[idx], "deps_link"] = trim(mvars[mconfs[idx], "deps_link"])
mvars[mconfs[idx], "deps_target"] = trim(mvars[mconfs[idx], "deps_target"])
}
# output all object lists rules for a specific .mconf file
function put_objlist(mconf, cobj, aobj, output)
{
printf("# object files list\n") >> output
printf("%s_OBJ := \\\n", mvars[mconf, "name"]) >> output
split(cobj, objs, " ")
for (o in objs) {
printf("\t%s \\\n", objs[o]) >> output
}
split(aobj, objs, " ")
for (o in objs) {
printf("\t%s \\\n", objs[o]) >> output
}
split(mvars[mconf, "dobj"], objs, " ")
for (o in objs) {
printf("\t%s \\\n", objs[o]) >> output
}
print "" >> output
}
# output all suffix (build) rules for a specific .mconf file
function put_suffix_rules(cobj, aobj, mconf, output)
{
printf("# suffix rules (metarules missing from most variants)\n") >> output
split(cobj, objs, " ")
for (o in objs) {
# prepare the source file name `s' out of `o'
s = objs[o]
gsub(/\.o$/, ".c", s)
gsub(/^\$\(BUILDDIR\)\//, "", s)
# fix up the target template when building generated source files
if (match(s, /^\$\(BUILDDIR\)\//) == 1) {
tmpl = envar["tmpldir"] "/" "suffix_bldir.tmpl"
n = split(s, gen, "/")
while (getline < tmpl > 0) {
gsub(/__OBJ__/, objs[o], $0)
gsub(/__SRC__/, s, $0)
gsub(/__GENSRC__/, "[GEN] " gen[n], $0)
gsub(/__CFLAGS__/, mvars[mconf, "cflags"], $0)
# write the result down in the current fragment
if ($0 != "") {
$0 = trim($0)
printf("%s\n", $0) >> output
}
}
close(tmpl)
} else {
tmpl = envar["tmpldir"] "/" "suffix.tmpl"
while (getline < tmpl > 0) {
gsub(/__OBJ__/, objs[o], $0)
gsub(/__SRC__/, s, $0)
gsub(/__CFLAGS__/, mvars[mconf, "cflags"], $0)
# write the result down in the current fragment
if ($0 != "") {
$0 = trim($0)
printf("%s\n", $0) >> output
}
}
close(tmpl)
}
}
split(aobj, objs, " ")
for (o in objs) {
# prepare the source file name `s' out of `o'
s = objs[o]
gsub(/\.o$/, ".S", s)
gsub(/^\$\(BUILDDIR\)\//, "", s)
# fix up the target template when building generated source files
if (match(s, /^\$\(BUILDDIR\)\//) == 1) {
tmpl = envar["tmpldir"] "/" "suffix_bldir.tmpl"
n = split(s, gen, "/")
while (getline < tmpl > 0) {
gsub(/__OBJ__/, objs[o], $0)
gsub(/__SRC__/, s, $0)
gsub(/__GENSRC__/, "[GEN] " gen[n], $0)
gsub(/__CFLAGS__/, mvars[mconf, "cflags"], $0)
# write the result down in the current fragment
if ($0 != "") {
$0 = trim($0)
printf("%s\n", $0) >> output
}
}
close(tmpl)
} else {
tmpl = envar["tmpldir"] "/" "suffix.tmpl"
while (getline < tmpl > 0) {
gsub(/__OBJ__/, objs[o], $0)
gsub(/__SRC__/, s, $0)
gsub(/__CFLAGS__/, mvars[mconf, "cflags"], $0)
# write the result down in the current fragment
if ($0 != "") {
$0 = trim($0)
printf("%s\n", $0) >> output
}
}
close(tmpl)
}
}
split(mvars[mconf, "dobj"], objs, " ")
for (o in objs) {
# prepare the source file name `s' out of `o'
s = objs[o]
gsub(/\.bin$/, "", s)
gsub(/^\$\(BUILDDIR\)\//, "", s)
tmpl = envar["tmpldir"] "/" "suffix_data.tmpl"
n = split(s, gen, "/")
while (getline < tmpl > 0) {
gsub(/__OBJ__/, objs[o], $0)
gsub(/__SRC__/, s, $0)
# write the result down in the current fragment
if ($0 != "") {
$0 = trim($0)
printf("%s\n", $0) >> output
}
}
close(tmpl)
}
print "" >> output
}
# output a "prog" target
function put_prog(mconf, mconfdir, output)
{
tmpl = envar["tmpldir"] "/" "prog.tmpl"
prefix = ""
printf("# link the program `%s'\n", mvars[mconf, "target"]) >> output
while (getline < tmpl > 0) {
gsub(/__TARGET__/, mvars[mconf, "target"], $0)
gsub(/__PATH__/, mconfdir, $0)
gsub(/__NAME__/, mvars[mconf, "name"], $0)
gsub(/__DEPEND_T__/, mvars[mconf, "deps_target"], $0)
gsub(/__DEPEND_L__/, mvars[mconf, "deps_link"], $0)
gsub(/__LDFLAGS__/, mvars[mconf, "ldflags"], $0)
gsub(/__EXTRALIBS__/, mvars[mconf, "extralibs"], $0)
$0 = trim($0)
printf("%s\n", $0) >> output
}
close(tmpl)
print "" >> output
mvars[mconf, "cleanfiles"] = mvars[mconf, "cleanfiles"] " " "$(" mvars[mconf, "target"] ")"
}
# output a "lib" target
function put_lib(mconf, mconfdir, output)
{
tmpl = envar["tmpldir"] "/" "lib.tmpl"
printf("# create lib `%s'\n", mvars[mconf, "target"]) >> output
while (getline < tmpl > 0) {
gsub(/__TARGET__/, mvars[mconf, "target"], $0)
gsub(/__PATH__/, mconfdir, $0)
gsub(/__NAME__/, mvars[mconf, "name"], $0)
$0 = trim($0)
printf("%s\n", $0) >> output
}
close(tmpl)
print "" >> output
mvars[mconf, "cleanfiles"] = mvars[mconf, "cleanfiles"] " " "$(" mvars[mconf, "target"] ")"
}
# output a "data" target
function put_data(mconf, mconfdir, output)
{
tmpl = envar["tmpldir"] "/" "data.tmpl"
printf("# create embedded data object `%s'\n", mvars[mconf, "target"]) >> output
while (getline < tmpl > 0) {
gsub(/__TARGET__/, mvars[mconf, "target"], $0)
gsub(/__PATH__/, mconfdir, $0)
gsub(/__NAME__/, mvars[mconf, "name"], $0)
$0 = trim($0)
printf("%s\n", $0) >> output
}
close(tmpl)
print "" >> output
mvars[mconf, "cleanfiles"] = mvars[mconf, "cleanfiles"] " " "$(" mvars[mconf, "target"] ")"
}
# generate 1 .mk file for specified .mconf -- to be inlined in top Makefile
function put_mkfile(mconf, mconfdir, output)
{
# gather objects from C files
cob = mvars[mconf, "cobj"]
if (envar["host"] == "unix")
cob = cob " " mvars[mconf, "unix_cobj"]
if (envar["host"] == "windows")
cob = cob " " mvars[mconf, "win_cobj"]
# gather objects from assembly .S files
aob = mvars[mconf, "aobj"]
if (envar["host"] == "unix")
aob = aob " " mvars[mconf, "unix_aobj"]
if (envar["host"] == "windows")
aob = aob " " mvars[mconf, "win_aobj"]
# write list of objects to build
put_objlist(mconf, cob, aob, output)
# if the mconf module is a program, write link rules
if (mvars[mconf, "prog"] != "")
put_prog(mconf, mconfdir, output)
# if the mconf module is a library, write lib creation rules
if (mvars[mconf, "lib"] != "")
put_lib(mconf, mconfdir, output)
# if the mconf module is embedded data objects, write data object creation rules
if (mvars[mconf, "data"] != "")
put_data(mconf, mconfdir, output)
# write each object suffix build rules
put_suffix_rules(cob, aob, mconf, output)
}
# generate the all: rule starting with loose targets, then libs, then programs in that order
# then generate the CLEANFILES rule
function genallrule(output)
{
combined_mconfsfile = envar["makeitgendir"] "/combined-mconfsready.mk"
reset_file(combined_mconfsfile)
printf("all:") > output
# write targets that are NOT libraries of programs first
for (m in mconfs) {
if (mvars[mconfs[m], "prog"] == "" && mvars[mconfs[m], "lib"] == "") {
printf(" $(%s)", mvars[mconfs[m], "target"]) >> output
put_mkfile(mconfs[m], mconfsdirs[m], combined_mconfsfile)
}
}
# then libraries
for (m in mconfs) {
if (mvars[mconfs[m], "lib"] != "") {
printf(" $(%s)", mvars[mconfs[m], "target"]) >> output
put_mkfile(mconfs[m], mconfsdirs[m], combined_mconfsfile)
}
}
# finally programs
for (m in mconfs) {
if (mvars[mconfs[m], "prog"] != "") {
printf(" $(%s)", mvars[mconfs[m], "target"]) >> output
put_mkfile(mconfs[m], mconfsdirs[m], combined_mconfsfile)
}
}
# collect cleanfiles from all mconfs and set a CLEANFILES make var
for (m in mconfs) {
cl = cl " " mvars[mconfs[m], "cleanfiles"] " "
}
cl = trim(cl)
printf("\n\nCLEANFILES += %s\n", cl) >> output
}
# check the parameters passed to the program
function checkvars()
{
if (envar["mconflist"] == "")
usage()
if (envar["host"] == "")
usage()
if (envar["makeitgendir"] == "")
usage()
if (envar["tmpldir"] == "")
usage()
}
# main entry
BEGIN {
# variable defs
mconfs[0] = ""
mconfsdirs[0] = ""
nmconfs = 0
mvars[0] = ""
words[0] = ""
currkeywd = ""
envar[0] = ""
keywords[0] = ""
klist = "name prog lib asrc data csrc win_asrc win_csrc unix_asrc unix_csrc"
klist = klist " " "depends cflags ldflags extralibs cleanfiles"
# init keywords
split(klist, keywords, " ")
# collect program environment vars from command line ARGV array
for (i = 0; i < ARGC; i++) {
n = split(ARGV[i], args, "=")
if (n == 2)
envar[args[1]] = args[2]
}
# check that we were called with all needed environment vars
checkvars()
# extract mconf dirname and basename from mconfig generated module.lst
while (getline < envar["mconflist"] > 0) {
mconfs[nmconfs] = $1 "/" $2
mconfsdirs[nmconfs] = $1
nmconfs++
}
# for all .mconf files found, 1) parse, 2) gen src/obj lists, 3) gen make targets
for (i = 0; i < nmconfs; i++) {
scanmconf(mconfs[i], envar["makeitgendir"])
genobjs(mconfs[i])
gentarget(mconfs[i])
}
# for each make targets generated above, generate dependency rules
for (i = 0; i < nmconfs; i++) {
gendeps(i)
}
# finally, generate the "all:" rule listing all target in dependency order
genallrule(envar["makeitgendir"] "/" "all.mk")
# printmvars()
}
|