Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
13 changes: 12 additions & 1 deletion .gdn/.gdnsuppress
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"default": {
"name": "default",
"createdDate": "2024-02-15 00:04:36Z",
"lastUpdatedDate": "2024-02-15 00:04:36Z"
"lastUpdatedDate": "2024-03-06 12:15:43Z"
}
},
"results": {
Expand Down Expand Up @@ -56,6 +56,17 @@
],
"justification": "Unable to resolve BA2007 identified in external LLVM CMake configuration.",
"createdDate": "2024-02-15 00:04:36Z"
},
"02dc21533e1f4f27353d0c34284a2ebf6d7357c7f7d0f9879fed4993728069e3": {
"signature": "02dc21533e1f4f27353d0c34284a2ebf6d7357c7f7d0f9879fed4993728069e3",
"alternativeSignatures": [
"6b50835be7f05c09d9c68d8cc7e3a0e3316c84e040b919b2ab7dbc4e228323fe"
],
"memberOf": [
"default"
],
"justification": "Unable to resolve BA2007 identified in external LLVM CMake configuration for llvm-objcopy.exe.",
"createdDate": "2024-03-06 12:15:43Z"
}
}
}
10 changes: 2 additions & 8 deletions build-llvm.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ set SOURCE_DIR=%MY_DIR%external\llvm\llvm

set PROJECTS=lld
set TARGETS=X86;ARM;AArch64
set BINARIES=llvm-mc.exe llvm-strip.exe lld.exe llc.exe
set PDBS=llvm-mc.pdb llvm-strip.pdb lld.pdb llc.pdb
set BINARIES=llvm-mc.exe llvm-objcopy.exe lld.exe llc.exe
set PDBS=llvm-mc.pdb llvm-objcopy.pdb lld.pdb llc.pdb

set HOST_BUILD_DIR=%BUILD_DIR%\%HOST%\llvm
set HOST_BIN_DIR=%HOST_BUILD_DIR%\Release\bin
Expand Down Expand Up @@ -68,12 +68,6 @@ IF %ERRORLEVEL% GEQ 1 EXIT /B 4
msbuild /p:Configuration=Release /m tools\llc\llc.vcxproj
IF %ERRORLEVEL% GEQ 1 EXIT /B 5

move %HOST_BIN_DIR%\llvm-objcopy.exe %HOST_BIN_DIR%\llvm-strip.exe
IF %ERRORLEVEL% GEQ 1 EXIT /B 6

move %HOST_BIN_DIR%\llvm-objcopy.pdb %HOST_BIN_DIR%\llvm-strip.pdb
IF %ERRORLEVEL% GEQ 1 EXIT /B 7

for %%b in (%BINARIES%) DO (
copy %HOST_BIN_DIR%\%%b %HOST_ARTIFACTS_BIN_DIR%\%%b
IF %ERRORLEVEL% GEQ 1 EXIT /B 8
Expand Down
1 change: 0 additions & 1 deletion build-llvm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ function build()
ninja -j${JOBS} lld
ninja -j${JOBS} llc

mv "${HOST_BIN_DIR}/llvm-objcopy" "${HOST_BIN_DIR}/llvm-strip"
grep 'CMAKE_PROJECT_VERSION:' "${MY_BUILD_DIR}/CMakeCache.txt" | cut -d '=' -f 2 > "${LLVM_VERSION_FILE}"

for b in ${LLVM_BINARIES}; do
Expand Down
14 changes: 13 additions & 1 deletion build-xa-utils.cmd
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ cmake --version
cmake -G "%CMAKE_VS_GENERATOR%" -A x64 ^
-DCMAKE_BUILD_TYPE=Release ^
-DCMAKE_MSVC_RUNTIME_LIBRARY="%MSVC_RUNTIME_LIBRARY%" ^
-DBINUTILS_VERSION="2.38" ^
-DBINUTILS_VERSION="2.42" ^
%SOURCE_DIR%
IF %ERRORLEVEL% GEQ 1 EXIT /B 1

Expand All @@ -27,4 +27,16 @@ IF %ERRORLEVEL% GEQ 1 EXIT /B 3
copy %HOST_BIN_DIR%\as.pdb %HOST_ARTIFACTS_BIN_DIR%\as.pdb
IF %ERRORLEVEL% GEQ 1 EXIT /B 4

copy %HOST_BIN_DIR%\llvm-strip.exe %HOST_ARTIFACTS_BIN_DIR%\llvm-strip.exe
IF %ERRORLEVEL% GEQ 1 EXIT /B 3

copy %HOST_BIN_DIR%\llvm-strip.pdb %HOST_ARTIFACTS_BIN_DIR%\llvm-strip.pdb
IF %ERRORLEVEL% GEQ 1 EXIT /B 4

copy %HOST_BIN_DIR%\ld.exe %HOST_ARTIFACTS_BIN_DIR%\ld.exe
IF %ERRORLEVEL% GEQ 1 EXIT /B 3

copy %HOST_BIN_DIR%\ld.pdb %HOST_ARTIFACTS_BIN_DIR%\ld.pdb
IF %ERRORLEVEL% GEQ 1 EXIT /B 4

cd %MY_DIR%
28 changes: 3 additions & 25 deletions build-xa-utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function configure()
set -x
cmake -G Ninja \
-DCMAKE_BUILD_TYPE="${CONFIGURATION}" \
-DBINUTILS_VERSION="2.38" \
-DBINUTILS_VERSION="2.42" \
-DLLVM_VERSION="${LLVM_VERSION}" \
"$@" \
"${SOURCE_DIR}"
Expand All @@ -39,37 +39,15 @@ function configure_darwin()
-DCMAKE_OSX_ARCHITECTURES='arm64;x86_64'
}

function configure_windows()
{
configure -DCMAKE_SYSTEM_NAME="Windows" \
-DCMAKE_CROSSCOMPILING="True" \
-DCMAKE_C_COMPILER="x86_64-w64-mingw32-gcc" \
-DCMAKE_CXX_COMPILER="x86_64-w64-mingw32-g++" \
-DCMAKE_RC_COMPILER="x86_64-w64-mingw32-windres" \
-DCMAKE_FIND_ROOT_PATH_MODE_PROGRAM="NEVER" \
-DCMAKE_FIND_ROOT_PATH_MODE_LIBRARY="ONLY" \
-DCMAKE_FIND_ROOT_PATH_MODE_INCLUDE="ONLY"
}

function build()
{
local host="${1}"

ninja -j${JOBS}

local exe
local cmd
if [ "${host}" == "windows" ]; then
exe=".exe"
cmd=".cmd"
fi

for b in ${XA_UTILS_BINARIES}; do
cp -P -a "${HOST_BIN_DIR}/${b}${exe}" "${HOST_ARTIFACTS_BIN_DIR}/${b}${exe}"

if [ "${host}" != "windows" ]; then
strip "${HOST_ARTIFACTS_BIN_DIR}/${b}"
fi
cp -P -a "${HOST_BIN_DIR}/${b}" "${HOST_ARTIFACTS_BIN_DIR}/${b}"
strip "${HOST_ARTIFACTS_BIN_DIR}/${b}"

if [ "${host}" == "linux" ]; then
compress_binary "${HOST_ARTIFACTS_BIN_DIR}/${b}"
Expand Down
4 changes: 2 additions & 2 deletions common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ ARTIFACTS_DIR="${MY_DIR}/artifacts"
HOST_ARTIFACTS_DIR="${ARTIFACTS_DIR}/${HOST}"
HOST_ARTIFACTS_BIN_DIR="${HOST_ARTIFACTS_DIR}/bin"
HOST_ARTIFACTS_LIB_DIR="${HOST_ARTIFACTS_DIR}/lib"
LLVM_BINARIES="llvm-mc llvm-strip lld llc"
LLVM_BINARIES="llvm-mc llvm-objcopy lld llc"
LLVM_PREFIXED_BINARIES="$(make_prefixed_binaries strip) $(make_prefixed_binaries ld)"
XA_UTILS_BINARIES="as"
XA_UTILS_BINARIES="as llvm-strip ld"
XA_UTILS_PREFIXED_BINARIES="$(make_prefixed_binaries as)"
BINARIES="${LLVM_BINARIES} ${LLVM_PREFIXED_BINARIES} ${XA_UTILS_BINARIES} ${XA_UTILS_PREFIXED_BINARIES}"
OPERATING_SYSTEMS="linux darwin windows"
Expand Down
6 changes: 1 addition & 5 deletions package.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,7 @@ function prepare()
for b in ${BINARIES}; do
src_pdb=""
dest_pdb=""
if [ "${b}" == "lld" ]; then
dest_b="ld"
else
dest_b="${b}"
fi
dest_b="${b}"

if [ -f "${artifacts_source_bin}/${b}${exe}" ]; then
src_pdb="${b}.pdb"
Expand Down
3 changes: 3 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ endif()
add_compile_definitions(XA_UTILS_VERSION="${PROJECT_VERSION}")
add_compile_definitions(BINUTILS_VERSION="${BINUTILS_VERSION}")
add_compile_definitions(LLVM_VERSION="${LLVM_VERSION}")
add_compile_definitions(CXXOPTS_NO_REGEX)

add_compile_options("$<$<COMPILE_LANGUAGE:CXX>:${XA_UTILS_CXX_FLAGS}>")
add_compile_options("$<$<COMPILE_LANGUAGE:C>:${XA_UTILS_C_FLAGS}>")
Expand All @@ -217,3 +218,5 @@ if(WIN32)
endif()

add_subdirectory(gas)
add_subdirectory(llvm-strip)
add_subdirectory(ld)
11 changes: 6 additions & 5 deletions src/gas/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ set(GAS_DRIVER_SOURCES
llvm_mc_runner_x64.cc
llvm_mc_runner_x86.cc
main.cc
process.cc
../shared/app.cc
../shared/process.cc
)

set(ARCH_PREFIXES
Expand All @@ -18,13 +19,13 @@ set(ARCH_PREFIXES

if(WIN32)
list(APPEND GAS_DRIVER_SOURCES
gas.windows.cc
process.windows.cc
../shared/app.windows.cc
../shared/process.windows.cc
)
else()
list(APPEND GAS_DRIVER_SOURCES
gas.posix.cc
process.posix.cc
../shared/app.posix.cc
../shared/process.posix.cc
)
endif()

Expand Down
10 changes: 2 additions & 8 deletions src/gas/constants.hh
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,20 @@
#if !defined (__CONSTANTS_HH)
#define __CONSTANTS_HH

#include "../shared/shared-constants.hh"

namespace xamarin::android::gas
{
class Constants final
{
public:
#if defined (_WIN32)
static constexpr char newline[] = "\r\n";
static constexpr char llvm_mc_name[] = "llvm-mc.exe";
#else
static constexpr char newline[] = "\n";
static constexpr char llvm_mc_name[] = "llvm-mc";
#endif
static constexpr char arch_hack_param[] = "@gas-arch=";
static constexpr char default_output_name[] = "a.out";
static constexpr int wrapper_general_error_code = 100;
static constexpr int wrapper_llvm_mc_killed_error_code = wrapper_general_error_code + 1;
static constexpr int wrapper_llvm_mc_stopped_error_code = wrapper_general_error_code + 2;
static constexpr int wrapper_fork_failed_error_code = wrapper_general_error_code + 3;
static constexpr int wrapper_exec_failed_error_code = wrapper_general_error_code + 4;
static constexpr int wrapper_wait_failed_error_code = wrapper_general_error_code + 5;
};
}
#endif
74 changes: 31 additions & 43 deletions src/gas/gas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@

#include "constants.hh"
#include "gas.hh"
#include "../shared/process.hh"
#include "llvm_mc_runner.hh"

using namespace xamarin::android::binutils;
using namespace xamarin::android::gas;

cxxopts::Options Gas::create_options ()
Expand Down Expand Up @@ -59,26 +61,17 @@ cxxopts::Options Gas::create_options ()
return options;
}

int Gas::usage (bool is_error, std::string const message)
void Gas::usage_print_extra ([[maybe_unused]] bool is_error)
{
if (!message.empty ()) {
std::cerr << message << Constants::newline << Constants::newline;
}

cxxopts::Options options = create_options ();

std::cerr << options.help () << std::endl << std::endl;
std::cerr << "`" << program_name () << "` takes a subset of arguments accepted by the GNU Assembler (gas) program." << Constants::newline
<< "Accepted options are limited to the ones used by Xamarin.Android and Mono/dotnet AOT, more can be added as-needed." << Constants::newline
<< "Some options are accepted but ignored, either because they are ignored by GAS as well or because they are used by" << Constants::newline
<< "Xamarin.Android or Mono/dotnet AOT, but not needed by `llvm-mc`. All the unsupported options will cause the wrapper" << Constants::newline
<< "to fail with an error message." << Constants::newline
<< "Since `llvm-mc` does not support compiling multiple source files at the same time, this GAS behavior is emulated" << Constants::newline
<< "by running `llvm-mc` once per each input file and using the `ld` linker in the end to merge all the discrete output" << Constants::newline
<< "files into the single file indicated by the `-o` option." << Constants::newline << Constants::newline
<< "Command line options are compatibile with GAS version " << BINUTILS_VERSION << Constants::newline << Constants::newline;

return is_error ? 1 : 0;
std::cerr << "`" << program_name () << "` takes a subset of arguments accepted by the GNU Assembler (gas) program." << SharedConstants::newline
<< "Accepted options are limited to the ones used by Xamarin.Android and Mono/dotnet AOT, more can be added as-needed." << SharedConstants::newline
<< "Some options are accepted but ignored, either because they are ignored by GAS as well or because they are used by" << SharedConstants::newline
<< "Xamarin.Android or Mono/dotnet AOT, but not needed by `llvm-mc`. All the unsupported options will cause the wrapper" << SharedConstants::newline
<< "to fail with an error message." << SharedConstants::newline
<< "Since `llvm-mc` does not support compiling multiple source files at the same time, this GAS behavior is emulated" << SharedConstants::newline
<< "by running `llvm-mc` once per each input file and using the `ld` linker in the end to merge all the discrete output" << SharedConstants::newline
<< "files into the single file indicated by the `-o` option." << SharedConstants::newline << SharedConstants::newline
<< "Command line options are compatibile with GAS version " << BINUTILS_VERSION << SharedConstants::newline << SharedConstants::newline;
}

int Gas::run (int argc, char **argv)
Expand All @@ -100,7 +93,7 @@ int Gas::run (int argc, char **argv)
arch_name = first_param + (sizeof(Constants::arch_hack_param) - 1);
lowercase_string (arch_name);
}
_program_name = arch_name;
set_program_name (arch_name);

std::unique_ptr<LlvmMcRunner> mc_runner;
std::string ld_name;
Expand All @@ -125,21 +118,21 @@ int Gas::run (int argc, char **argv)
message
.append (generic_gas_name)
.append ("), please use one of the ABI-prefixed names:")
.append (Constants::newline)
.append (" ").append (arm64_gas_name.data ()).append (Constants::newline)
.append (" ").append (arm32_gas_name.data ()).append (Constants::newline)
.append (" ").append (x86_gas_name.data ()).append (Constants::newline)
.append (" ").append (x64_gas_name.data ()).append (Constants::newline);
.append (SharedConstants::newline)
.append (" ").append (arm64_gas_name.data ()).append (SharedConstants::newline)
.append (" ").append (arm32_gas_name.data ()).append (SharedConstants::newline)
.append (" ").append (x86_gas_name.data ()).append (SharedConstants::newline)
.append (" ").append (x64_gas_name.data ()).append (SharedConstants::newline);
return usage (true /* is_error */, message);
} else {
std::string message { "Unknown program name '" };
message.append (arch_name).append ("'").append (Constants::newline);
message.append (arch_name).append ("'").append (SharedConstants::newline);
return usage (true /* is_error */, message);
}

auto&& [terminate, is_error] = parse_arguments (argc, argv, mc_runner);
if (terminate || is_error) {
return is_error ? Constants::wrapper_general_error_code : 0;
return is_error ? SharedConstants::wrapper_general_error_code : 0;
}

fs::path llvm_mc = program_dir () / Constants::llvm_mc_name;
Expand Down Expand Up @@ -186,9 +179,9 @@ int Gas::run (int argc, char **argv)
ld->append_program_argument (_gas_output_file.empty () ? Constants::default_output_name : _gas_output_file.string ());
ld->append_program_argument ("--relocatable");

std::cout << "Have multiple output files:" << Constants::newline;
std::cout << "Have multiple output files:" << SharedConstants::newline;
for (fs::path const& output : output_files) {
std::cout << " " << output << Constants::newline;
std::cout << " " << output << SharedConstants::newline;
ld->append_program_argument (output.string ());
}

Expand All @@ -198,33 +191,32 @@ int Gas::run (int argc, char **argv)
return 0;
}

Gas::ParseArgsResult Gas::parse_arguments (int argc, char **argv, std::unique_ptr<LlvmMcRunner>& mc_runner)
ParseArgsResult Gas::parse_arguments (int argc, char **argv, std::unique_ptr<LlvmMcRunner>& mc_runner)
{
cxxopts::Options options = create_options ();
bool terminate = false, is_error = false;
bool show_version = false;

options.parse_positional ("asmfile");
auto result = options.parse(argc, argv);
if (result.count ("help") > 0) {
exit (usage (false /* is_error */));
}

bool is_error = false;
bool show_version = false;

if (result.count ("version") > 0) {
show_version = true;
terminate = true;
}

if (result.count ("V") > 0) {
show_version = true;
terminate = false;
}

if (show_version) {
std::cout << program_name () << " v" << XA_UTILS_VERSION << ", " << PROGRAM_DESCRIPTION << Constants::newline
<< "\tGAS version compatibility: " << BINUTILS_VERSION << Constants::newline
<< "\tllvm-mc version compatibility: " << LLVM_VERSION << Constants::newline << Constants::newline;
return {true, false};
std::cout << program_name () << " v" << XA_UTILS_VERSION << ", " << PROGRAM_DESCRIPTION << SharedConstants::newline
<< "\tGAS version compatibility: " << BINUTILS_VERSION << SharedConstants::newline
<< "\tllvm-mc version compatibility: " << LLVM_VERSION << SharedConstants::newline << SharedConstants::newline;
return {true /* terminate */, false /* is_error */};
}

if (result.count ("gen-debug") > 0) {
Expand All @@ -239,10 +231,6 @@ Gas::ParseArgsResult Gas::parse_arguments (int argc, char **argv, std::unique_pt
_gas_output_file = result["o"].as<std::string> ();
}

if (terminate) {
return {terminate, is_error};
}

if (_gas_output_file.empty ()) {
_gas_output_file = Constants::default_output_name;
}
Expand All @@ -256,5 +244,5 @@ Gas::ParseArgsResult Gas::parse_arguments (int argc, char **argv, std::unique_pt
}
}

return {terminate, is_error};
return {false /* terminate */, is_error};
}
Loading