Skip to content

Commit

Permalink
[build] cmake support auto build
Browse files Browse the repository at this point in the history
  • Loading branch information
YuzukiTsuru committed Dec 27, 2023
1 parent 13ee2b0 commit 237c596
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 44 deletions.
7 changes: 4 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ include(ExternalProject)

# Define the path to the board loader files
set(BOARD_FLOADER_PATH ${CMAKE_SOURCE_DIR}/cmake/board/)
# Define the path to the mksunxi tool
set(CMAKE_MKSUNXI "${CMAKE_SOURCE_DIR}/tools/mksunxi")
# Define the path to the bin2array tool
set(CMAKE_BIN2ARRAY "${CMAKE_SOURCE_DIR}/tools/bin2array")

# Check if the CMAKE_BOARD_FILE variable is defined
if(CMAKE_BOARD_FILE)
Expand Down Expand Up @@ -93,9 +97,6 @@ set(CMAKE_AR "${CROSS_COMPILE}ar")
set(CMAKE_OBJCOPY "${CROSS_COMPILE}objcopy")
set(CMAKE_SIZE "${CROSS_COMPILE}size")

# Define the path to the mksunxi tool
set(CMAKE_MKSUNXI "${PROJECT_SOURCE_DIR}/tools/mksunxi")

# Define the paths to the linker scripts
set(LINK_SCRIPT_FEL ${PROJECT_BINARY_DIR}/link_elf.ld)
set(LINK_SCRIPT_BIN ${PROJECT_BINARY_DIR}/link_bin.ld)
Expand Down
17 changes: 14 additions & 3 deletions cmake/board/longanpi-3h.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,16 @@
set(CONFIG_ARCH_ARM32 True)
set(CONFIG_ARCH_ARM32_ARM64 True)
set(CONFIG_CHIP_SUN50IW9 True)
set(CONFIG_USE_DRAM_PAYLOAD True)
set(CONFIG_BOARD_LONGANPI-3H True)

add_definitions(-DCONFIG_CHIP_SUN50IW9)

set(CONFIG_USE_DRAM_PAYLOAD True)
set(CONFIG_USE_DRAM_PAYLOAD_SOURCE_PATH "${CMAKE_SOURCE_DIR}/payloads/sun50iw9_libdram")
set(CONFIG_USE_DRAM_PAYLOAD_BIN_PATH "${CONFIG_USE_DRAM_PAYLOAD_SOURCE_PATH}/output/ddr.bin")
set(CONFIG_USE_DRAM_PAYLOAD_FILE_PATH "${CMAKE_SOURCE_DIR}/board/longanpi-3h/payloads/init_dram_bin.c")
set(CONFIG_USE_DRAM_PAYLOAD_SECTION "init_dram_bin")

# Set the cross-compile toolchain
set(CROSS_COMPILE "arm-linux-gnueabi-")
set(CROSS_COMPILE ${CROSS_COMPILE} CACHE STRING "CROSS_COMPILE Toolchain")
Expand All @@ -32,9 +37,15 @@ set(ARCH_FEL_SRAM_LENGTH "100K")
ExternalProject_Add(
init_dram
PREFIX init_dram
SOURCE_DIR "${CMAKE_SOURCE_DIR}/payloads/sun50iw9_libdram"
SOURCE_DIR "${CONFIG_USE_DRAM_PAYLOAD_SOURCE_PATH}"
INSTALL_COMMAND ""
CONFIGURE_COMMAND ""
BUILD_COMMAND make -C ${CMAKE_SOURCE_DIR}/payloads/sun50iw9_libdram
BUILD_COMMAND make -C ${CONFIG_USE_DRAM_PAYLOAD_SOURCE_PATH}
BUILD_IN_SOURCE 1
)

add_custom_command(
TARGET init_dram
POST_BUILD COMMAND ${CMAKE_BIN2ARRAY} ${CONFIG_USE_DRAM_PAYLOAD_BIN_PATH} ${CONFIG_USE_DRAM_PAYLOAD_FILE_PATH} ${CONFIG_USE_DRAM_PAYLOAD_SECTION}
COMMENT "Generate DRAM LIB Payload ${CONFIG_USE_DRAM_PAYLOAD_FILE_PATH} for ${CONFIG_USE_DRAM_PAYLOAD_FILE_PATH}"
)
14 changes: 7 additions & 7 deletions tools/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,23 @@ all: tools
tools: $(BUILD_DIR) $(MKSUNXI) $(BINTOARR)

$(BUILD_DIR):
mkdir -p $(BUILD_DIR)
@mkdir -p $(BUILD_DIR)

clean:
rm -rf build
rm -f $(MKSUNXI)
rm -f $(BINTOARR)

$(BUILD_DIR)/%.o : %.c
echo " CC $<"
$(CC) $(CFLAGS) -c $< -o $@
@echo " CC $<"
@$(CC) $(CFLAGS) -c $< -o $@

$(BUILD_DIR)/%.opp : %.cpp
echo " CXX $<"
$(CXX) $(CXXFLAGS) -c $< -o $@
@echo " CXX $<"
@$(CXX) $(CXXFLAGS) -c $< -o $@

$(MKSUNXI): $(MKSUNXI_COBJS)
$(CC) $(CFLAGS) $(BUILD_DIR)/mksunxi.o -o $(MKSUNXI)
@$(CC) $(CFLAGS) $(BUILD_DIR)/mksunxi.o -o $(MKSUNXI)

$(BINTOARR): $(BINTOARR_COBJS)
$(CC) $(CFLAGS) $(BUILD_DIR)/bin2array.o -o $(BINTOARR)
@$(CC) $(CFLAGS) $(BUILD_DIR)/bin2array.o -o $(BINTOARR)
55 changes: 24 additions & 31 deletions tools/bin2array.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,78 +2,71 @@
#include <stdlib.h>
#include <string.h>

#define BYTES_PER_LINE 16
#define BYTES_PER_LINE 16// Number of bytes to display per line in the output C array

int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Usage: %s input_file output_file\n", argv[0]);
if (argc != 4) {
printf("Usage: %s input_file output_file\n", argv[0]);// Print usage information if the number of command-line arguments is not 3
return 1;
}

FILE *inputFile, *outputFile;
char *inputFileName = argv[1]; // 输入的二进制文件名
char *outputFileName = argv[2];// 输出的C数组文件名
char *inputFileName = argv[1]; // Name of the input binary file
char *outputFileName = argv[2];// Name of the output C array file
char *funcName = argv[3];// Name of the output C array
int fileSize;

// 打开输入的二进制文件
// Open the input binary file
inputFile = fopen(inputFileName, "rb");
if (inputFile == NULL) {
printf("无法打开输入文件\n");
printf("Unable to open input file\n");// Print an error message if the input file cannot be opened
return 1;
}

// 提取输出文件的名称作为C数组的名称
char *arrayName = strrchr(outputFileName, '/');
if (arrayName == NULL) {
arrayName = strtok(outputFileName, ".");
} else {
arrayName++;// 跳过文件名分隔符
}

// 获取文件大小
// Get the size of the file
fseek(inputFile, 0, SEEK_END);
fileSize = ftell(inputFile);
fseek(inputFile, 0, SEEK_SET);

// 创建输出的C数组文件
// Create the output C array file
outputFile = fopen(outputFileName, "w");
if (outputFile == NULL) {
printf("无法创建输出文件\n");
printf("Unable to create output file\n");// Print an error message if the output file cannot be created
fclose(inputFile);
return 1;
}

// 写入C数组声明到输出文件
fprintf(outputFile, "const unsigned char __attribute__((section(\".%s\"))) %s[%d] = {\n\t", arrayName, arrayName, fileSize);
// Write the C array declaration to the output file
fprintf(outputFile, "const unsigned char __attribute__((section(\".%s\"))) %s[%d] = {\n\t", funcName, funcName, fileSize);

// 逐字节读取二进制文件并写入C数组文件
// Read the binary file byte by byte and write to the C array file
for (int i = 0; i < fileSize; i++) {
unsigned char byte;
if (fread(&byte, 1, 1, inputFile) != 1) {
printf("读取文件时发生错误\n");
printf("Error occurred while reading the file\n");// Print an error message if there is an error reading the file
fclose(inputFile);
fclose(outputFile);
return 1;
}
fprintf(outputFile, "0x%02X", byte);// 以十六进制格式写入字节值
fprintf(outputFile, "0x%02X", byte);// Write the byte value in hexadecimal format
if (i < fileSize - 1) {
fprintf(outputFile, ", ");// 写入逗号分隔符
fprintf(outputFile, ", ");// Write a comma as a separator
}
if ((i + 1) % BYTES_PER_LINE == 0) {
fprintf(outputFile, "\n\t");// 每16个字节换行
fprintf(outputFile, "\n\t");// Move to a new line every 16 bytes
}
}

fprintf(outputFile, "\n};");// 结尾加上分号
fprintf(outputFile, "\n};");// Add a semicolon at the end

// 写入C数组声明到输出文件
fprintf(outputFile, "\n\nunsigned long long %s_length = %d;\n\t", arrayName, fileSize);
// Write the C array length declaration to the output file
fprintf(outputFile, "\n\nunsigned long long %s_length = %d;\n\t", funcName, fileSize);

// 关闭文件
// Close the files
fclose(inputFile);
fclose(outputFile);

printf("转换完成\n");
printf("Conversion complete %s => %s\n", inputFileName, outputFileName);// Print a completion message

return 0;
}
}

0 comments on commit 237c596

Please sign in to comment.