Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
build/
.xmake/
.vscode/
77 changes: 77 additions & 0 deletions arm-gcc.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
toolchain("arm-gcc")

set_description("ARM Compiler of GCC")
set_kind("cross")

on_load(function(toolchain)
if toolchain:is_plat("windows") then
toolchain:set("toolset", "cc", "arm-none-eabi-gcc.exe")
toolchain:set("toolset", "ld", "arm-none-eabi-gcc.exe")
toolchain:set("toolset", "ar", "arm-none-eabi-ar.exe")
toolchain:set("toolset", "as", "arm-none-eabi-gcc.exe")
else
toolchain:set("toolset", "cc", "arm-none-eabi-gcc")
toolchain:set("toolset", "ld", "arm-none-eabi-gcc")
toolchain:set("toolset", "ar", "arm-none-eabi-ar")
toolchain:set("toolset", "as", "arm-none-eabi-gcc")
end
end)

toolchain_end()

rule("generate-hex")
after_build(function(target)
local out = target:targetfile() or ""
local gen_fi = "build/" .. target:name()
print(string.format("%s => %s", out, gen_fi))

-- https://github.com/xmake-io/xmake/discussions/2125
-- os.exec("arm-none-eabi-objdump -S "..out.." > "..gen_fi..".asm")
-- local asm = os.iorun("arm-none-eabi-objdump -S build/cross/cortex-m4/release/minimal-proj")
-- io.writefile(gen_fi..".asm", asm)

if target:is_plat("windows") then
os.execv("arm-none-eabi-objcopy.exe", {"-Obinary", out, gen_fi .. ".bin"})
os.execv("arm-none-eabi-objdump.exe", {"-S", out}, {stdout = gen_fi .. ".asm"})
os.execv("arm-none-eabi-objcopy.exe", {"-O", "ihex", out, gen_fi .. ".hex"})
else
os.execv("arm-none-eabi-objcopy", {"-Obinary", out, gen_fi .. ".bin"})
os.execv("arm-none-eabi-objdump", {"-S", out}, {stdout = gen_fi .. ".asm"})
os.execv("arm-none-eabi-objcopy", {"-O", "ihex", out, gen_fi .. ".hex"})
end

os.mv(out, gen_fi .. ".elf") -- add .elf file
-- -I binary
-- $(Q) $(OBJ_COPY) -O ihex $@ $(BUILD_DIR)/$(TARGET).hex
-- $(Q) $(OBJ_COPY) -O binary $@ $(BUILD_DIR)/$(TARGET).bin
-- os.exec("qemu-system-arm -M stm32-p103 -nographic -kernel"..bin_out)
end)

after_clean(function(target)
local gen_fi = "build/" .. target:name()
os.rm(gen_fi .. ".*")
end)

rule_end()

task("qemu")

on_run(function()
print("Run binary in Qemu!")
local bin_out = os.files("$(buildir)/*.bin")[1]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use import("lib.detect.find_file") instead of os.files, and use core.project.config + config.buildir() instead of $(buildir)

if bin_out then
os.exec("qemu-system-arm -M stm32-p103 -nographic -kernel " .. bin_out)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

os.execv

else
print("Do not find bin file in $(buildir)/")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use raise()

end
end)

set_menu {
-- Settings menu usage
usage = "xmake qemu",

-- Setup menu description
description = "Run binary in Qemu!"
}

task_end()
84 changes: 0 additions & 84 deletions armgcc.lua

This file was deleted.

26 changes: 21 additions & 5 deletions xmake.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,31 @@ set_project("stm32-demo")
set_plat("cross")
set_arch("cortex-m4")

includes("armgcc.lua")
local arm_gcc_installing_path = os.getenv("ARM_GCC_TOOL") or "/Applications/ARM/"
use_toolchain(arm_gcc_installing_path)
includes("arm-gcc.lua")

target("minimal-proj")
set_kind("binary")
add_files("src/*.c")
add_rules("arm-gcc")
add_ldflags("-Tsrc/main.ld")

add_cflags(
"-specs=nano.specs", "-mcpu=cortex-m4", "-mthumb",
"-mfloat-abi=hard -mfpu=fpv4-sp-d16",
"-fdata-sections -ffunction-sections",
"-nostartfiles","-Os"
)

add_ldflags(
"-specs=nano.specs", "-mcpu=cortex-m4", "-mthumb",
"-mfloat-abi=hard -mfpu=fpv4-sp-d16",
"-fdata-sections -ffunction-sections",
"-nostartfiles","-Os",
"-Wl,--gc-sections",
"-Tsrc/main.ld", {force = true}
)

add_links("m", "c", "nosys")
add_rules("generate-hex")


-- If you want to known more usage about xmake, please see https://xmake.io
-- Reference Project:
Expand Down