Skip to content
Merged
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
9 changes: 1 addition & 8 deletions prepare-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ MY_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
source common.sh

WORK_DIR="${MY_DIR}/prep"
ARTIFACT_TARBALL="${DIST_PACKAGE_NAME_BASE}.tar.bz2"
ARTIFACTS_DIR="${WORK_DIR}/artifacts"
ARTIFACTS_DIR="${WORK_DIR}"

ARTIFACT_ZIP="${1}"
XA_TAG_COMPONENT="${2}"
Expand Down Expand Up @@ -38,12 +37,6 @@ function prepare()
echo Unpacking artifact ZIP
unzip "${ARTIFACT_ZIP}"

if [ ! -f "${ARTIFACT_TARBALL}" ]; then
die Build artifact tarball $(pwd)/${ARTIFACT_TARBALL} not found
fi

echo Unpacking binaries tarball
tar xf "${ARTIFACT_TARBALL}"
if [ ! -d "${ARTIFACTS_DIR}" ]; then
die Artifacts directory ${ARTIFACTS_DIR} does not exist
fi
Expand Down
13 changes: 5 additions & 8 deletions src/gas/command_line.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <vector>

#include "command_line.hh"
#include "exceptions.hh"
#include "platform.hh"

using namespace xamarin::android::gas;
Expand Down Expand Up @@ -34,7 +33,8 @@ bool CommandLine::parse (std::span<const CommandLineOption> options, std::vector
if (last_opt.has_value ()) {
option_cb (last_opt.value (), option);
} else {
throw invalid_argument_error { "Option '" + option + "' requires an argument." };
STDERR << "Option '" << option << "' requires an argument.";
return false;
}

next_arg_is_value = false;
Expand Down Expand Up @@ -83,7 +83,7 @@ bool CommandLine::parse (std::span<const CommandLineOption> options, std::vector
auto match = std::find_if (options.begin (), options.end (), matching_option);
#endif
if (match == options.end ()) {
STDERR << "Uncrecognized option '" << option << "'\n";
STDERR << "Unrecognized option '" << option << Constants::newline;
continue;
}

Expand All @@ -98,11 +98,8 @@ bool CommandLine::parse (std::span<const CommandLineOption> options, std::vector
}

if (last_opt.has_value ()) {
platform::string message { "Option '" };
message
.append (last_opt.value ().name)
.append ("' requires an argument.");
throw invalid_operation_error {message};
STDERR << "Option '" << last_opt.value().name << "' requires an argument." << std::endl;
return false;
}

return true;
Expand Down
4 changes: 2 additions & 2 deletions src/gas/command_line.hh
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ namespace xamarin::android::gas

#define CLISTR(_str_lit_) PSTR((_str_lit_))

#if !defined (_WIN32)
#define CLIPARAM(_str_lit_) std::string_view { _str_lit_ }
#if defined (_WIN32)
#define CLIPARAM(_str_lit_) std::wstring_view { L ## _str_lit_ }
#else
#define CLIPARAM(_str_lit_) std::string_view { _str_lit_ }
#endif
Expand Down
6 changes: 3 additions & 3 deletions src/gas/gas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ int Gas::usage (bool is_error, platform::string const message)
return is_error ? 1 : 0;
}

std::vector<platform::string> Gas::get_command_line (int &argc, char **&argv)
std::vector<platform::string> Gas::get_command_line (int argc, argv_char **argv)
{
std::vector<platform::string> ret;

Expand Down Expand Up @@ -173,11 +173,11 @@ int Gas::run (std::vector<platform::string> args)
ld_path /= ld_name;
auto ld = std::make_unique<Process> (ld_path);
ld->append_program_argument (PSTR("-o"));
ld->append_program_argument (_gas_output_file.empty () ? platform::string (Constants::default_output_name) : _gas_output_file.string ());
ld->append_program_argument (_gas_output_file.empty () ? platform::string (Constants::default_output_name) : _gas_output_file.native ());
ld->append_program_argument (PSTR("--relocatable"));

for (fs::path const& output : output_files) {
ld->append_program_argument (output.string ());
ld->append_program_argument (output.native ());
}

return ld->run ();
Expand Down
4 changes: 3 additions & 1 deletion src/gas/gas.hh
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ namespace xamarin::android::gas
~Gas ()
{}

std::vector<platform::string> get_command_line (int &argc, char **&argv);
void dump_command_line_args (int argc, argv_char **argv);
static void platform_setup ();
std::vector<platform::string> get_command_line (int argc, argv_char **argv);

int run (std::vector<platform::string> args);

Expand Down
6 changes: 6 additions & 0 deletions src/gas/gas.posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@

using namespace xamarin::android::gas;

void Gas::dump_command_line_args ([[maybe_unused]] int argc, [[maybe_unused]] char **argv)
{}

void Gas::platform_setup()
{}

void Gas::determine_program_dir (std::vector<platform::string> args)
{
fs::path program_path { args[0] };
Expand Down
63 changes: 63 additions & 0 deletions src/gas/gas.windows.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,79 @@
#include <windows.h>
#include <shlwapi.h>

#include <cstdio>
#include <clocale>
#include <cstring>
#include <iostream>
#include <vector>
#include <io.h>
#include <fcntl.h>

#include "exceptions.hh"
#include "gas.hh"
#include "platform.hh"

using namespace xamarin::android::gas;

namespace {
void log_cp_info(std::wstring label, UINT cp)
{
std::wcout << L" " << label << L" code page:" << std::endl;
std::wcout << L" ID: " << cp << std::endl;

CPINFOEX cpinfo;
BOOL result = GetCPInfoEx(cp, 0, &cpinfo);
if (!result) {
std::wcout << L" failed to obtain more information about the code page" << std::endl;
return;
}

std::wcout
<< L" Maximum character size: " << cpinfo.MaxCharSize << std::endl
<< L" Localized name: " << cpinfo.CodePageName << std::endl;
}

void log_cli_arg(int index, const wchar_t* arg)
{
std::wcout << L" [" << index << L"] " << std::endl;
std::wcout << std::endl;
std::wstring ws(arg);
std::wcout << L" As C string (direct): " << ws << std::endl;
std::wcout << L" As hex bytes: " << std::hex << std::setw(4) << std::setfill(L'0');

const wchar_t* p = arg;
while (p != nullptr && *p != 0) {
auto ch = static_cast<uint16_t>(*p);
std::wcout << ch << " ";
p++;
}
std::wcout << std::endl;
}
}

void Gas::dump_command_line_args (int argc, wchar_t **argv)
{
std::wcout << L"Active code pages information" << std::endl;
log_cp_info(L"OS", GetACP());
std::wcout << std::endl;
log_cp_info(L"OEM", GetOEMCP());
std::wcout << std::endl;

std::wcout << L"Command line arguments (" << argc << "):" << std::endl;
for (int i = 0; i < argc; i++) {
log_cli_arg(i, argv[i]);
}
std::wcout << L"================================" << std::endl << std::endl;
}

void Gas::platform_setup()
{
// Windows needs that magic to make stdout work with wchar_t and friends
constexpr char cp_utf16le[] = ".1200"; // UTF-16 little-endian locale.
setlocale(LC_ALL, cp_utf16le);
_setmode(_fileno(stdout), _O_WTEXT);
}

void Gas::determine_program_dir (std::vector<platform::string> args)
{
TCHAR buffer[MAX_PATH + 1]{};
Expand Down
6 changes: 3 additions & 3 deletions src/gas/llvm_mc_runner.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ std::unordered_map<LlvmMcArgument, bool> LlvmMcRunner::known_options {
int LlvmMcRunner::run (fs::path const& executable_path)
{
if (!fs::exists (executable_path)) {
STDERR << "Executable '" << executable_path.string () << "' does not exist." << Constants::newline;
STDERR << "Executable '" << executable_path.native () << "' does not exist." << Constants::newline;
return Constants::wrapper_exec_failed_error_code;
}

Expand Down Expand Up @@ -59,8 +59,8 @@ int LlvmMcRunner::run (fs::path const& executable_path)
process->append_program_argument (PSTR("-o"), opt->second);
}

platform::string input_file { PSTR("\"") + input_file_path.make_preferred ().string () + PSTR("\"") };
process->append_program_argument (input_file_path.make_preferred ().string ());
platform::string input_file { PSTR("\"") + input_file_path.make_preferred ().native () + PSTR("\"") };
process->append_program_argument (input_file_path.make_preferred ().native ());

return process->run ();
}
4 changes: 2 additions & 2 deletions src/gas/llvm_mc_runner.hh
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ namespace xamarin::android::gas

void set_output_file_path (fs::path const& file_path)
{
set_option (LlvmMcArgument::Output, file_path.string ());
set_option (LlvmMcArgument::Output, file_path.native ());
}

void add_include_path (fs::path const& include_path)
Expand All @@ -77,7 +77,7 @@ namespace xamarin::android::gas
return;
}

set_option (LlvmMcArgument::IncludeDir, include_path.string ());
set_option (LlvmMcArgument::IncludeDir, include_path.native ());
}

void generate_debug_info ()
Expand Down
6 changes: 5 additions & 1 deletion src/gas/main.cc
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
// SPDX-License-Identifier: MIT
#include <iostream>
#include <vector>

#include "gas.hh"
#include "platform.hh"

#if defined(_WIN32)
int wmain (int argc, wchar_t **argv)
#else
int main (int argc, char **argv)
#endif
{
xamarin::android::gas::Gas::platform_setup ();
xamarin::android::gas::Gas app;

std::vector<platform::string> args = app.get_command_line (argc, argv);
Expand Down
16 changes: 9 additions & 7 deletions src/gas/platform.hh
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,23 @@
#include <iostream>

#if defined(_WIN32)
#define STDOUT std::cout
#define STDERR std::cerr
#define PSTR(_str_lit_) (_str_lit_)
#define PCHAR(_ch_) (_ch_)
#define STDOUT std::wcout
#define STDERR std::wcerr
#define PSTR(_str_lit_) (L ## _str_lit_)
#define PCHAR(_ch_) (L ## _ch_)
using argv_char = wchar_t;
#else
#define STDOUT std::cout
#define STDERR std::cerr
#define PSTR(_str_lit_) (_str_lit_)
#define PCHAR(_ch_) (_ch_)
using argv_char = char;
#endif

namespace platform {
#if !defined (_WIN32)
using string = std::string;
using string_view = std::string_view;
#if defined (_WIN32)
using string = std::wstring;
using string_view = std::wstring_view;
#else
using string = std::string;
using string_view = std::string_view;
Expand Down
31 changes: 14 additions & 17 deletions src/gas/process.windows.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ int Process::run (bool print_command_line)
print_process_command_line ();
}

platform::string binary = executable_path.string ();
platform::string binary = executable_path.native ();
platform::string args { escape_argument (binary) };
for (platform::string const& a : _args) {
if (a.empty ()) {
Expand All @@ -59,21 +59,14 @@ int Process::run (bool print_command_line)
args.append (escape_argument (a));
}

int size = MultiByteToWideChar (CP_UTF8, 0, args.c_str (), -1, NULL , 0);
wchar_t* wargs = new wchar_t [size];
MultiByteToWideChar (CP_UTF8, 0, args.c_str (), -1, wargs, size);

size = MultiByteToWideChar (CP_UTF8, 0, binary.c_str (), -1, NULL , 0);
wchar_t* wbinary = new wchar_t [size];
MultiByteToWideChar (CP_UTF8, 0, binary.c_str (), -1, wbinary, size);

PROCESS_INFORMATION pi {};
STARTUPINFOW si {};
si.cb = sizeof(si);

DWORD creation_flags = CREATE_UNICODE_ENVIRONMENT;
wchar_t* wargs = _wcsdup(args.c_str());
BOOL success = CreateProcessW (
wbinary,
binary.c_str (),
wargs,
nullptr, // process security attributes
nullptr, // primary thread security attributes
Expand All @@ -84,24 +77,28 @@ int Process::run (bool print_command_line)
&si,
&pi
);

delete[] wargs;
delete[] wbinary;
free (wargs);

if (!success) {
return Constants::wrapper_exec_failed_error_code;
}

// TODO: error handling below
int ret = 0;
DWORD result = WaitForSingleObject (pi.hProcess, INFINITE);
if (result == 0) {
DWORD retcode = 0;
if (GetExitCodeProcess (pi.hProcess, &retcode)) {
return retcode;
ret = retcode;
} else {
ret = 128;
}

return 128;
} else {
ret = 1;
}

return 1;
CloseHandle (pi.hProcess);
CloseHandle (pi.hThread);

return ret;
}