1*af03003cSMatthias Ringwaldimport os 2*af03003cSMatthias Ringwald 3*af03003cSMatthias Ringwaldc, link, asm, utils = emk.module("c", "link", "asm", "utils") 4*af03003cSMatthias Ringwald 5*af03003cSMatthias Ringwalddefault_compile_flags = ["-fvisibility=hidden", "-Wall", "-Wextra", "-Wshadow", "-Werror", "-Wno-missing-field-initializers", "-Wno-unused-parameter", \ 6*af03003cSMatthias Ringwald "-Wno-comment", "-Wno-unused", "-Wno-unknown-pragmas"] 7*af03003cSMatthias Ringwalddefault_link_flags = [] 8*af03003cSMatthias Ringwaldopt_flags = {"dbg":["-g"], "std":["-O2"], "max":["-O3"], "small":["-Os"]} 9*af03003cSMatthias Ringwaldopt_link_flags = {"dbg":[], "std":[], "max":[], "small":[]} 10*af03003cSMatthias Ringwaldc_flags = ["-std=c99"] 11*af03003cSMatthias Ringwaldcxx_flags = ["-std=c++11", "-Wno-reorder", "-fno-rtti", "-fno-exceptions"] 12*af03003cSMatthias Ringwaldc_link_flags = [] 13*af03003cSMatthias Ringwaldcxx_link_flags = ["-fno-rtti", "-fno-exceptions"] 14*af03003cSMatthias Ringwald 15*af03003cSMatthias Ringwalddef setup_build_dir(): 16*af03003cSMatthias Ringwald build_arch = None 17*af03003cSMatthias Ringwald if "arch" in emk.options: 18*af03003cSMatthias Ringwald build_arch = emk.options["arch"] 19*af03003cSMatthias Ringwald elif not emk.cleaning: 20*af03003cSMatthias Ringwald build_arch = "osx" 21*af03003cSMatthias Ringwald emk.options["arch"] = build_arch 22*af03003cSMatthias Ringwald 23*af03003cSMatthias Ringwald opt_level = None 24*af03003cSMatthias Ringwald if "opt" in emk.options: 25*af03003cSMatthias Ringwald level = emk.options["opt"] 26*af03003cSMatthias Ringwald if level in opt_flags: 27*af03003cSMatthias Ringwald opt_level = level 28*af03003cSMatthias Ringwald else: 29*af03003cSMatthias Ringwald emk.log.warning("Unknown optimization level '%s'" % (level)) 30*af03003cSMatthias Ringwald elif not emk.cleaning: 31*af03003cSMatthias Ringwald opt_level = "dbg" 32*af03003cSMatthias Ringwald emk.options["opt"] = opt_level 33*af03003cSMatthias Ringwald 34*af03003cSMatthias Ringwald dirs = ["__build__"] 35*af03003cSMatthias Ringwald if build_arch: 36*af03003cSMatthias Ringwald dirs.append(build_arch) 37*af03003cSMatthias Ringwald if opt_level: 38*af03003cSMatthias Ringwald dirs.append(opt_level) 39*af03003cSMatthias Ringwald emk.build_dir = os.path.join(*dirs) 40*af03003cSMatthias Ringwald 41*af03003cSMatthias Ringwalddef setup_osx(): 42*af03003cSMatthias Ringwald global c 43*af03003cSMatthias Ringwald global link 44*af03003cSMatthias Ringwald 45*af03003cSMatthias Ringwald flags = [("-arch", "x86_64"), "-fno-common", "-Wnewline-eof"] 46*af03003cSMatthias Ringwald c.flags.extend(flags) 47*af03003cSMatthias Ringwald c.cxx.flags += ["-stdlib=libc++"] 48*af03003cSMatthias Ringwald link.cxx.flags += ["-stdlib=libc++"] 49*af03003cSMatthias Ringwald 50*af03003cSMatthias Ringwald link_flags = [("-arch", "x86_64")] 51*af03003cSMatthias Ringwald link.local_flags.extend(link_flags) 52*af03003cSMatthias Ringwald 53*af03003cSMatthias Ringwalddef setup_avr(): 54*af03003cSMatthias Ringwald global c 55*af03003cSMatthias Ringwald global link 56*af03003cSMatthias Ringwald 57*af03003cSMatthias Ringwald c.compiler = c.GccCompiler("/Projects/avr-tools/bin/avr-") 58*af03003cSMatthias Ringwald c.flags += ["-mmcu=atmega256rfr2", "-ffunction-sections", "-fdata-sections"] 59*af03003cSMatthias Ringwald link.linker = link.GccLinker("/Projects/avr-tools/bin/avr-") 60*af03003cSMatthias Ringwald link.flags += ["-mmcu=atmega256rfr2", "-mrelax", "-Wl,--gc-sections"] 61*af03003cSMatthias Ringwald link.strip = True 62*af03003cSMatthias Ringwald 63*af03003cSMatthias Ringwalddef setup_arm_thumb(): 64*af03003cSMatthias Ringwald global c 65*af03003cSMatthias Ringwald global link 66*af03003cSMatthias Ringwald global asm 67*af03003cSMatthias Ringwald global utils 68*af03003cSMatthias Ringwald 69*af03003cSMatthias Ringwald asm.assembler = asm.GccAssembler("/cross/arm_cortex/bin/arm-none-eabi-") 70*af03003cSMatthias Ringwald c.compiler = c.GccCompiler("/cross/arm_cortex/bin/arm-none-eabi-") 71*af03003cSMatthias Ringwald link.linker = link.GccLinker("/cross/arm_cortex/bin/arm-none-eabi-") 72*af03003cSMatthias Ringwald 73*af03003cSMatthias Ringwald c.flags.extend(["-mcpu=cortex-m0", "-mthumb", "-ffunction-sections", "-fdata-sections", "-fno-builtin-fprintf", "-fno-builtin-printf"]) 74*af03003cSMatthias Ringwald c.defines["LPC11XX"] = 1 75*af03003cSMatthias Ringwald 76*af03003cSMatthias Ringwald link.local_flags.extend(["-mcpu=cortex-m0", "-mthumb", "-nostartfiles", "-nostdlib", "-Wl,--gc-sections"]) 77*af03003cSMatthias Ringwald link.local_flags.extend(["-Tflash.lds", "-L/Projects/lpc11xx/core", "/Projects/lpc11xx/core/" + emk.build_dir + "/board_cstartup.o"]) 78*af03003cSMatthias Ringwald link.local_syslibs += ["gcc"] 79*af03003cSMatthias Ringwald link.depdirs += ["/Projects/lpc11xx/stdlib"] 80*af03003cSMatthias Ringwald 81*af03003cSMatthias Ringwald def do_objcopy(produces, requires): 82*af03003cSMatthias Ringwald utils.call("/cross/arm_cortex/bin/arm-none-eabi-objcopy", "-O", "binary", requires[0], produces[0]) 83*af03003cSMatthias Ringwald 84*af03003cSMatthias Ringwald def handle_exe(path): 85*af03003cSMatthias Ringwald emk.depend(path, "/Projects/lpc11xx/core/" + emk.build_dir + "/board_cstartup.o") 86*af03003cSMatthias Ringwald emk.rule(do_objcopy, path + ".bin", path, cwd_safe=True, ex_safe=True) 87*af03003cSMatthias Ringwald emk.autobuild(path + ".bin") 88*af03003cSMatthias Ringwald 89*af03003cSMatthias Ringwald link.exe_funcs.append(handle_exe) 90*af03003cSMatthias Ringwald link.strip = True 91*af03003cSMatthias Ringwald 92*af03003cSMatthias Ringwald emk.recurse("/Projects/lpc11xx/core") 93*af03003cSMatthias Ringwald 94*af03003cSMatthias Ringwalddef setup_linux_rpi(): 95*af03003cSMatthias Ringwald global c 96*af03003cSMatthias Ringwald global link 97*af03003cSMatthias Ringwald 98*af03003cSMatthias Ringwald c.compiler = c.GccCompiler("/Volumes/xtools/arm-none-linux-gnueabi/bin/arm-none-linux-gnueabi-") 99*af03003cSMatthias Ringwald link.linker = link.GccLinker("/Volumes/xtools/arm-none-linux-gnueabi/bin/arm-none-linux-gnueabi-") 100*af03003cSMatthias Ringwald 101*af03003cSMatthias Ringwald c.flags.extend(["-fomit-frame-pointer"]) 102*af03003cSMatthias Ringwald 103*af03003cSMatthias Ringwaldsetup_build_dir() 104*af03003cSMatthias Ringwald 105*af03003cSMatthias Ringwaldsetup_funcs = {"osx":setup_osx, "avr":setup_avr, "arm_thumb":setup_arm_thumb, "rpi": setup_linux_rpi} 106*af03003cSMatthias Ringwald 107*af03003cSMatthias Ringwaldif not emk.cleaning: 108*af03003cSMatthias Ringwald build_arch = emk.options["arch"] 109*af03003cSMatthias Ringwald opt_level = emk.options["opt"] 110*af03003cSMatthias Ringwald 111*af03003cSMatthias Ringwald c.flags.extend(default_compile_flags) 112*af03003cSMatthias Ringwald c.flags.extend(opt_flags[opt_level]) 113*af03003cSMatthias Ringwald c.c.flags.extend(c_flags) 114*af03003cSMatthias Ringwald c.cxx.flags.extend(cxx_flags) 115*af03003cSMatthias Ringwald link.local_flags.extend(default_link_flags) 116*af03003cSMatthias Ringwald link.local_flags.extend(opt_link_flags[opt_level]) 117*af03003cSMatthias Ringwald link.c.local_flags.extend(c_link_flags) 118*af03003cSMatthias Ringwald link.cxx.local_flags.extend(cxx_link_flags) 119*af03003cSMatthias Ringwald 120*af03003cSMatthias Ringwald c.include_dirs.append("$:proj:$") 121*af03003cSMatthias Ringwald 122*af03003cSMatthias Ringwald if build_arch in setup_funcs: 123*af03003cSMatthias Ringwald setup_funcs[build_arch]() 124*af03003cSMatthias Ringwald else: 125*af03003cSMatthias Ringwald raise emk.BuildError("Unknown target arch '%s'" % (build_arch)) 126*af03003cSMatthias Ringwald 127*af03003cSMatthias Ringwald c.defines["TARGET_ARCH_" + build_arch.upper()] = 1 128