Advertisement
Guest User

N64 MakeElf

a guest
May 17th, 2024
889
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.63 KB | None | 1 0
  1. #!/bin/bash
  2.  
  3. # Set the basename from the input file without the extension
  4. BASENAME=$(basename "$1" .z64)
  5.  
  6. # Define directories
  7. BUILD_DIR=build
  8. ASM_DIR=asm
  9. BIN_DIR=assets
  10. SRC_DIR=src
  11. TOOLS_DIR=tools
  12.  
  13. # Define toolchain
  14. CROSS=mips-linux-gnu-
  15. AS=${CROSS}as
  16. LD=${CROSS}ld
  17. OBJCOPY=${CROSS}objcopy
  18. PYTHON=python3
  19.  
  20. # Define flags
  21. OBJCOPYFLAGS="-O binary"
  22. LDFLAGS="-T ${BASENAME}.ld -Map ${BUILD_DIR}/${BASENAME}.map -T undefined_syms_auto.txt -T undefined_funcs_auto.txt --no-check-sections"
  23.  
  24. # Define compiler and assembler flags
  25. CC=${TOOLS_DIR}/ido_recomp/linux/5.3/cc
  26. OPT_FLAGS="-O2"
  27. MIPSISET="-mips1 -o32"
  28. INCLUDE_CFLAGS="-I . -I include"
  29. ASFLAGS="-EB -mtune=vr4300 -march=vr4300 -mabi=32 -I include"
  30.  
  31. # Ensure the macro.inc file exists in the asm directory
  32. if [ ! -f $ASM_DIR/macro.inc ]; then
  33. echo "macro.inc not found in $ASM_DIR!"
  34. exit 1
  35. fi
  36.  
  37. # Ensure the macro.inc file exists in the asm/data directory
  38. if [ ! -f $ASM_DIR/data/macro.inc ]; then
  39. echo "macro.inc not found in $ASM_DIR/data!"
  40. exit 1
  41. fi
  42.  
  43. # Function to convert .bin to .o
  44. convert_bin_to_o() {
  45. for bin_file in $(find ${BIN_DIR} -name "*.bin"); do
  46. bin_base=$(basename ${bin_file} .bin)
  47. mkdir -p $(dirname ${BUILD_DIR}/${bin_file})
  48. ${LD} -r -b binary -o ${BUILD_DIR}/${bin_file%.*}.o ${bin_file}
  49. if [ $? -ne 0 ]; then
  50. echo "Error: objcopy failed for ${bin_file}"
  51. exit 1
  52. fi
  53. done
  54. }
  55.  
  56. # Function to assemble .s files to .o files
  57. assemble_s_files() {
  58. for asm_file in $(find ${ASM_DIR} -name "*.s"); do
  59. asm_base=$(basename ${asm_file} .s)
  60. mkdir -p $(dirname ${BUILD_DIR}/${asm_file})
  61. ${AS} ${ASFLAGS} -o ${BUILD_DIR}/${asm_file%.*}.o ${asm_file}
  62. if [ $? -ne 0 ]; then
  63. echo "Error: assembler failed for ${asm_file}"
  64. exit 1
  65. fi
  66. done
  67. }
  68.  
  69. # Function to compile .c files to .o files, if src directory exists
  70. compile_c_files() {
  71. if [ -d ${SRC_DIR} ]; then
  72. for c_file in $(find ${SRC_DIR} -name "*.c"); do
  73. c_base=$(basename ${c_file} .c)
  74. mkdir -p $(dirname ${BUILD_DIR}/${c_file})
  75. ${CC} -c ${CFLAGS} ${OPT_FLAGS} ${MIPSISET} -o ${BUILD_DIR}/${c_file%.*}.o ${c_file}
  76. if [ $? -ne 0 ]; then
  77. echo "Error: compiler failed for ${c_file}"
  78. exit 1
  79. fi
  80. done
  81. fi
  82. }
  83.  
  84. # Clean the build directory
  85. clean_build() {
  86. rm -rf ${BUILD_DIR}
  87. }
  88.  
  89. # Main function
  90. main() {
  91. # Clean previous build
  92. clean_build
  93.  
  94. # Create necessary directories
  95. mkdir -p ${BUILD_DIR}/${ASM_DIR} ${BUILD_DIR}/${BIN_DIR}
  96.  
  97. # Convert .bin to .o
  98. convert_bin_to_o
  99.  
  100. # Assemble .s files to .o files
  101. assemble_s_files
  102.  
  103. # Compile .c files to .o files
  104. compile_c_files
  105.  
  106. # Link all .o files to create the ELF file
  107. ${LD} ${LDFLAGS} -o ${BUILD_DIR}/${BASENAME}.elf $(find ${BUILD_DIR} -name "*.o")
  108.  
  109. if [ ! -f ${BUILD_DIR}/${BASENAME}.elf ]; then
  110. echo "Error: ELF file not created"
  111. exit 1
  112. fi
  113.  
  114. # Convert the ELF file to binary
  115. ${OBJCOPY} ${OBJCOPYFLAGS} ${BUILD_DIR}/${BASENAME}.elf ${BUILD_DIR}/${BASENAME}.bin
  116.  
  117. if [ ! -f ${BUILD_DIR}/${BASENAME}.bin ]; then
  118. echo "Error: Binary file not created"
  119. exit 1
  120. fi
  121.  
  122. # Copy the binary file to .z64
  123. cp ${BUILD_DIR}/${BASENAME}.bin ${BUILD_DIR}/${BASENAME}.z64
  124.  
  125. echo "Build completed: ${BUILD_DIR}/${BASENAME}.z64"
  126. }
  127.  
  128. # Check if the input file is provided
  129. if [ -z "$1" ]; then
  130. echo "Usage: $0 <path-to-.z64-file>"
  131. exit 1
  132. fi
  133.  
  134. # Run the main function
  135. main "$1"
  136.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement