mirror of
https://github.com/ziglang/zig.git
synced 2025-01-31 14:21:14 +00:00
Merge remote-tracking branch 'origin/master' into llvm6
This commit is contained in:
commit
5d9a8cbe1a
@ -570,6 +570,14 @@ set(ZIG_C_HEADER_FILES
|
||||
"xtestintrin.h"
|
||||
)
|
||||
|
||||
if(MSVC)
|
||||
set(MSVC_DIA_SDK_DIR "$ENV{VSINSTALLDIR}DIA SDK")
|
||||
if (IS_DIRECTORY ${MSVC_DIA_SDK_DIR})
|
||||
set(ZIG_DIA_GUIDS_LIB "${MSVC_DIA_SDK_DIR}/lib/amd64/diaguids.lib")
|
||||
string(REGEX REPLACE "\\\\" "\\\\\\\\" ZIG_DIA_GUIDS_LIB_ESCAPED "${ZIG_DIA_GUIDS_LIB}")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
set(ZIG_LIB_DIR "lib/zig")
|
||||
set(C_HEADERS_DEST "${ZIG_LIB_DIR}/include")
|
||||
set(ZIG_STD_DEST "${ZIG_LIB_DIR}/std")
|
||||
@ -631,6 +639,10 @@ target_link_libraries(zig LINK_PUBLIC
|
||||
${LLVM_LIBRARIES}
|
||||
${CMAKE_THREAD_LIBS_INIT}
|
||||
)
|
||||
if(ZIG_DIA_GUIDS_LIB)
|
||||
target_link_libraries(zig LINK_PUBLIC ${ZIG_DIA_GUIDS_LIB})
|
||||
endif()
|
||||
|
||||
if(MSVC OR MINGW)
|
||||
target_link_libraries(zig LINK_PUBLIC version)
|
||||
endif()
|
||||
|
@ -154,8 +154,7 @@ make install
|
||||
`ZIG_LIBC_LIB_DIR` and `ZIG_LIBC_STATIC_LIB_DIR` are unused.
|
||||
|
||||
```
|
||||
brew install cmake
|
||||
brew install llvm@6
|
||||
brew install cmake llvm@6
|
||||
brew outdated llvm@6 || brew upgrade llvm@6
|
||||
mkdir build
|
||||
cd build
|
||||
|
132
build.zig
132
build.zig
@ -35,55 +35,67 @@ pub fn build(b: &Builder) {
|
||||
|
||||
const test_step = b.step("test", "Run all the tests");
|
||||
|
||||
if (findLLVM(b)) |llvm| {
|
||||
// find the stage0 build artifacts because we're going to re-use config.h and zig_cpp library
|
||||
const build_info = b.exec([][]const u8{b.zig_exe, "BUILD_INFO"});
|
||||
var index: usize = 0;
|
||||
const cmake_binary_dir = nextValue(&index, build_info);
|
||||
const cxx_compiler = nextValue(&index, build_info);
|
||||
const lld_include_dir = nextValue(&index, build_info);
|
||||
const lld_libraries = nextValue(&index, build_info);
|
||||
const std_files = nextValue(&index, build_info);
|
||||
const c_header_files = nextValue(&index, build_info);
|
||||
// find the stage0 build artifacts because we're going to re-use config.h and zig_cpp library
|
||||
const build_info = b.exec([][]const u8{b.zig_exe, "BUILD_INFO"});
|
||||
var index: usize = 0;
|
||||
const cmake_binary_dir = nextValue(&index, build_info);
|
||||
const cxx_compiler = nextValue(&index, build_info);
|
||||
const llvm_config_exe = nextValue(&index, build_info);
|
||||
const lld_include_dir = nextValue(&index, build_info);
|
||||
const lld_libraries = nextValue(&index, build_info);
|
||||
const std_files = nextValue(&index, build_info);
|
||||
const c_header_files = nextValue(&index, build_info);
|
||||
const dia_guids_lib = nextValue(&index, build_info);
|
||||
|
||||
var exe = b.addExecutable("zig", "src-self-hosted/main.zig");
|
||||
exe.setBuildMode(mode);
|
||||
exe.addIncludeDir("src");
|
||||
exe.addIncludeDir(cmake_binary_dir);
|
||||
addCppLib(b, exe, cmake_binary_dir, "zig_cpp");
|
||||
if (lld_include_dir.len != 0) {
|
||||
exe.addIncludeDir(lld_include_dir);
|
||||
var it = mem.split(lld_libraries, ";");
|
||||
while (it.next()) |lib| {
|
||||
exe.addObjectFile(lib);
|
||||
}
|
||||
} else {
|
||||
addCppLib(b, exe, cmake_binary_dir, "embedded_lld_elf");
|
||||
addCppLib(b, exe, cmake_binary_dir, "embedded_lld_coff");
|
||||
addCppLib(b, exe, cmake_binary_dir, "embedded_lld_lib");
|
||||
const llvm = findLLVM(b, llvm_config_exe);
|
||||
|
||||
var exe = b.addExecutable("zig", "src-self-hosted/main.zig");
|
||||
exe.setBuildMode(mode);
|
||||
exe.addIncludeDir("src");
|
||||
exe.addIncludeDir(cmake_binary_dir);
|
||||
addCppLib(b, exe, cmake_binary_dir, "zig_cpp");
|
||||
if (lld_include_dir.len != 0) {
|
||||
exe.addIncludeDir(lld_include_dir);
|
||||
var it = mem.split(lld_libraries, ";");
|
||||
while (it.next()) |lib| {
|
||||
exe.addObjectFile(lib);
|
||||
}
|
||||
dependOnLib(exe, llvm);
|
||||
} else {
|
||||
addCppLib(b, exe, cmake_binary_dir, "embedded_lld_elf");
|
||||
addCppLib(b, exe, cmake_binary_dir, "embedded_lld_coff");
|
||||
addCppLib(b, exe, cmake_binary_dir, "embedded_lld_lib");
|
||||
}
|
||||
dependOnLib(exe, llvm);
|
||||
|
||||
if (!exe.target.isWindows()) {
|
||||
const libstdcxx_path_padded = b.exec([][]const u8{cxx_compiler, "-print-file-name=libstdc++.a"});
|
||||
const libstdcxx_path = ??mem.split(libstdcxx_path_padded, "\r\n").next();
|
||||
exe.addObjectFile(libstdcxx_path);
|
||||
|
||||
exe.linkSystemLibrary("pthread");
|
||||
}
|
||||
|
||||
exe.linkSystemLibrary("c");
|
||||
|
||||
b.default_step.dependOn(&exe.step);
|
||||
b.default_step.dependOn(docs_step);
|
||||
test_step.dependOn(&exe.step);
|
||||
|
||||
b.installArtifact(exe);
|
||||
installStdLib(b, std_files);
|
||||
installCHeaders(b, c_header_files);
|
||||
if (exe.target.getOs() == builtin.Os.linux) {
|
||||
const libstdcxx_path_padded = b.exec([][]const u8{cxx_compiler, "-print-file-name=libstdc++.a"});
|
||||
const libstdcxx_path = ??mem.split(libstdcxx_path_padded, "\r\n").next();
|
||||
exe.addObjectFile(libstdcxx_path);
|
||||
|
||||
exe.linkSystemLibrary("pthread");
|
||||
} else if (exe.target.isDarwin()) {
|
||||
exe.linkSystemLibrary("c++");
|
||||
}
|
||||
|
||||
if (dia_guids_lib.len != 0) {
|
||||
exe.addObjectFile(dia_guids_lib);
|
||||
}
|
||||
|
||||
exe.linkSystemLibrary("c");
|
||||
|
||||
b.default_step.dependOn(&exe.step);
|
||||
b.default_step.dependOn(docs_step);
|
||||
|
||||
const skip_self_hosted = b.option(bool, "skip-self-hosted", "Main test suite skips building self hosted compiler") ?? false;
|
||||
if (!skip_self_hosted) {
|
||||
test_step.dependOn(&exe.step);
|
||||
}
|
||||
const verbose_link_exe = b.option(bool, "verbose-link", "Print link command for self hosted compiler") ?? false;
|
||||
exe.setVerboseLink(verbose_link_exe);
|
||||
|
||||
b.installArtifact(exe);
|
||||
installStdLib(b, std_files);
|
||||
installCHeaders(b, c_header_files);
|
||||
|
||||
const test_filter = b.option([]const u8, "test-filter", "Skip tests that do not match filter");
|
||||
const with_lldb = b.option(bool, "with-lldb", "Run tests in LLDB to get a backtrace if one fails") ?? false;
|
||||
@ -142,20 +154,7 @@ const LibraryDep = struct {
|
||||
includes: ArrayList([]const u8),
|
||||
};
|
||||
|
||||
fn findLLVM(b: &Builder) -> ?LibraryDep {
|
||||
const llvm_config_exe = b.findProgram(
|
||||
[][]const u8{"llvm-config-5.0", "llvm-config"},
|
||||
[][]const u8{
|
||||
"C:/Libraries/llvm-5.0.0/bin",
|
||||
"/c/msys64/mingw64/bin",
|
||||
"c:/msys64/mingw64/bin",
|
||||
"/usr/local/opt/llvm@5/bin",
|
||||
"/mingw64/bin",
|
||||
}) %% |err|
|
||||
{
|
||||
warn("unable to find llvm-config: {}\n", err);
|
||||
return null;
|
||||
};
|
||||
fn findLLVM(b: &Builder, llvm_config_exe: []const u8) -> LibraryDep {
|
||||
const libs_output = b.exec([][]const u8{llvm_config_exe, "--libs", "--system-libs"});
|
||||
const includes_output = b.exec([][]const u8{llvm_config_exe, "--includedir"});
|
||||
const libdir_output = b.exec([][]const u8{llvm_config_exe, "--libdir"});
|
||||
@ -223,8 +222,19 @@ pub fn installCHeaders(b: &Builder, c_header_files: []const u8) {
|
||||
|
||||
fn nextValue(index: &usize, build_info: []const u8) -> []const u8 {
|
||||
const start = *index;
|
||||
while (build_info[*index] != '\n' and build_info[*index] != '\r') : (*index += 1) { }
|
||||
const result = build_info[start..*index];
|
||||
*index += 1;
|
||||
return result;
|
||||
while (true) : (*index += 1) {
|
||||
switch (build_info[*index]) {
|
||||
'\n' => {
|
||||
const result = build_info[start..*index];
|
||||
*index += 1;
|
||||
return result;
|
||||
},
|
||||
'\r' => {
|
||||
const result = build_info[start..*index];
|
||||
*index += 2;
|
||||
return result;
|
||||
},
|
||||
else => continue,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,4 +6,4 @@ build_script:
|
||||
after_build:
|
||||
- '%APPVEYOR_BUILD_FOLDER%\ci\appveyor\after_build.bat'
|
||||
cache:
|
||||
- 'llvm+clang-5.0.0-win64-msvc-release.tar.xz'
|
||||
- 'llvm+clang-5.0.1-win64-msvc-release.tar.xz'
|
||||
|
@ -7,7 +7,7 @@ SET "PATH=C:\msys64\mingw64\bin;C:\msys64\usr\bin;%PATH%"
|
||||
SET "MSYSTEM=MINGW64"
|
||||
SET "APPVEYOR_CACHE_ENTRY_ZIP_ARGS=-m0=Copy"
|
||||
|
||||
bash -lc "cd ${APPVEYOR_BUILD_FOLDER} && if [ -s ""llvm+clang-6.0.0-win64-msvc-release.tar.xz"" ]; then echo 'skipping LLVM download'; else wget 'https://s3.amazonaws.com/superjoe/temp/llvm%%2bclang-6.0.0-win64-msvc-release.tar.xz'; fi && tar xf llvm+clang-6.0.0-win64-msvc-release.tar.xz" || exit /b
|
||||
bash -lc "cd ${APPVEYOR_BUILD_FOLDER} && if [ -s ""llvm+clang-6.0.0-win64-msvc-release.tar.xz"" ]; then echo 'skipping LLVM download'; else wget 'https://s3.amazonaws.com/ziglang.org/deps/llvm%%2bclang-6.0.0-win64-msvc-release.tar.xz'; fi && tar xf llvm+clang-6.0.0-win64-msvc-release.tar.xz" || exit /b
|
||||
|
||||
|
||||
SET "PATH=%PREVPATH%"
|
||||
@ -15,12 +15,16 @@ SET "MSYSTEM=%PREVMSYSTEM%"
|
||||
SET "ZIGBUILDDIR=%APPVEYOR_BUILD_FOLDER%\build-msvc-release"
|
||||
SET "ZIGPREFIXPATH=%APPVEYOR_BUILD_FOLDER%\llvm+clang-6.0.0-win64-msvc-release"
|
||||
|
||||
call "C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\SetEnv.cmd" /x64
|
||||
call "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\vcvarsall.bat" x86_amd64
|
||||
>>>>>>> origin/master
|
||||
|
||||
mkdir %ZIGBUILDDIR%
|
||||
cd %ZIGBUILDDIR%
|
||||
cmake.exe .. -Thost=x64 -G"Visual Studio 14 2015 Win64" "-DCMAKE_INSTALL_PREFIX=%ZIGBUILDDIR%" "-DCMAKE_PREFIX_PATH=%ZIGPREFIXPATH%" -DCMAKE_BUILD_TYPE=Release "-DZIG_LIBC_INCLUDE_DIR=C:\Program Files (x86)\Windows Kits\10\Include\10.0.10240.0\ucrt" "-DZIG_LIBC_LIB_DIR=C:\Program Files (x86)\Windows Kits\10\bin\x64\ucrt" "-DZIG_LIBC_STATIC_LIB_DIR=C:\Program Files (x86)\Windows Kits\10\Lib\10.0.10240.0\ucrt\x64" || exit /b
|
||||
msbuild /p:Configuration=Release INSTALL.vcxproj || exit /b
|
||||
|
||||
bin\zig.exe build --build-file ..\build.zig test || exit /b
|
||||
bin\zig.exe build --build-file ..\build.zig test -Dverbose-link || exit /b
|
||||
|
||||
@echo "MSVC build succeeded, proceeding with MinGW build"
|
||||
cd %APPVEYOR_BUILD_FOLDER%
|
||||
|
@ -9,17 +9,4 @@ cmake .. -DCMAKE_PREFIX_PATH=/usr/local/opt/llvm@5/ -DCMAKE_INSTALL_PREFIX=$(pwd
|
||||
make VERBOSE=1
|
||||
make install
|
||||
|
||||
# TODO: we run the tests separately because when run all together there is some
|
||||
# mysterious issue where after N child process spawns it crashes. I've been
|
||||
# unable to reproduce the issue on my macbook - it only happens on Travis.
|
||||
# ./zig build --build-file ../build.zig test
|
||||
|
||||
./zig build --build-file ../build.zig test-behavior --verbose
|
||||
./zig build --build-file ../build.zig test-std --verbose
|
||||
./zig build --build-file ../build.zig test-compiler-rt --verbose
|
||||
./zig build --build-file ../build.zig test-compare-output --verbose
|
||||
./zig build --build-file ../build.zig test-build-examples --verbose
|
||||
./zig build --build-file ../build.zig test-compile-errors --verbose
|
||||
./zig build --build-file ../build.zig test-asm-link --verbose
|
||||
./zig build --build-file ../build.zig test-debug-safety --verbose
|
||||
./zig build --build-file ../build.zig test-translate-c --verbose
|
||||
./zig build --build-file ../build.zig test
|
||||
|
@ -50,6 +50,7 @@
|
||||
</ul>
|
||||
<h2 id="reading-material">Reading Material</h2>
|
||||
<ul>
|
||||
<li>2018-01-03 - <a href="http://andrewkelley.me/post/zig-december-2017-in-review.html">December 2017 in Review</a></li>
|
||||
<li>2017-10-17 - <a href="download/0.1.1/release-notes.html">Zig 0.1.1 Release Notes</a></li>
|
||||
<li>2017-07-19 - <a href="http://tiehuis.github.io/iterative-replacement-of-c-with-zig">Iterative Replacement of C with Zig</a></li>
|
||||
<li>2017-02-16 - <a href="http://andrewkelley.me/post/a-better-way-to-implement-bit-fields.html">A Better Way to Implement Bit-Fields</a></li>
|
||||
|
@ -3397,7 +3397,7 @@ fn doAThing() -> ?&Foo {
|
||||
<pre><code class="zig">fn doAThing(nullable_foo: ?&Foo) {
|
||||
// do some stuff
|
||||
|
||||
if (const foo ?= nullable_foo) {
|
||||
if (nullable_foo) |foo| {
|
||||
doSomethingWithFoo(foo);
|
||||
}
|
||||
|
||||
|
@ -208,7 +208,7 @@ pub const Module = struct {
|
||||
|
||||
const root_src_path = self.root_src_path ?? @panic("TODO handle null root src path");
|
||||
const root_src_real_path = os.path.real(self.allocator, root_src_path) %% |err| {
|
||||
%return printError("unable to open '{}': {}", root_src_path, err);
|
||||
%return printError("unable to get real path '{}': {}", root_src_path, err);
|
||||
return err;
|
||||
};
|
||||
%defer self.allocator.free(root_src_real_path);
|
||||
|
@ -29,7 +29,9 @@
|
||||
#define ZIG_CXX_COMPILER "@CMAKE_CXX_COMPILER@"
|
||||
#define ZIG_LLD_INCLUDE_PATH "@LLD_INCLUDE_DIRS@"
|
||||
#define ZIG_LLD_LIBRARIES "@LLD_LIBRARIES@"
|
||||
#define ZIG_LLVM_CONFIG_EXE "@LLVM_CONFIG_EXE@"
|
||||
#define ZIG_STD_FILES "@ZIG_STD_FILES@"
|
||||
#define ZIG_C_HEADER_FILES "@ZIG_C_HEADER_FILES@"
|
||||
#define ZIG_DIA_GUIDS_LIB "@ZIG_DIA_GUIDS_LIB_ESCAPED@"
|
||||
|
||||
#endif
|
||||
|
@ -267,13 +267,15 @@ static void add_package(CodeGen *g, CliPkg *cli_pkg, PackageTableEntry *pkg) {
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
if (argc == 2 && strcmp(argv[1], "BUILD_INFO") == 0) {
|
||||
printf("%s\n%s\n%s\n%s\n%s\n%s\n",
|
||||
printf("%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n",
|
||||
ZIG_CMAKE_BINARY_DIR,
|
||||
ZIG_CXX_COMPILER,
|
||||
ZIG_LLVM_CONFIG_EXE,
|
||||
ZIG_LLD_INCLUDE_PATH,
|
||||
ZIG_LLD_LIBRARIES,
|
||||
ZIG_STD_FILES,
|
||||
ZIG_C_HEADER_FILES);
|
||||
ZIG_C_HEADER_FILES,
|
||||
ZIG_DIA_GUIDS_LIB);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -1055,6 +1055,11 @@ int os_find_windows_sdk(ZigWindowsSDK **out_sdk) {
|
||||
if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
|
||||
int c0 = 0, c1 = 0, c2 = 0, c3 = 0;
|
||||
sscanf(ffd.cFileName, "%d.%d.%d.%d", &c0, &c1, &c2, &c3);
|
||||
if (c0 == 10 && c1 == 0 && c2 == 10240 && c3 == 0) {
|
||||
// Microsoft released 26624 as 10240 accidentally.
|
||||
// https://developer.microsoft.com/en-us/windows/downloads/sdk-archive
|
||||
c2 = 26624;
|
||||
}
|
||||
if ((c0 > v0) || (c1 > v1) || (c2 > v2) || (c3 > v3)) {
|
||||
v0 = c0, v1 = c1, v2 = c2, v3 = c3;
|
||||
buf_init_from_str(&result_sdk->version10, ffd.cFileName);
|
||||
|
@ -842,10 +842,10 @@ pub const LibExeObjStep = struct {
|
||||
lib_paths: ArrayList([]const u8),
|
||||
disable_libc: bool,
|
||||
frameworks: BufSet,
|
||||
verbose_link: bool,
|
||||
|
||||
// zig only stuff
|
||||
root_src: ?[]const u8,
|
||||
verbose: bool,
|
||||
output_h_path: ?[]const u8,
|
||||
out_h_filename: []const u8,
|
||||
assembly_files: ArrayList([]const u8),
|
||||
@ -923,7 +923,7 @@ pub const LibExeObjStep = struct {
|
||||
var self = LibExeObjStep {
|
||||
.strip = false,
|
||||
.builder = builder,
|
||||
.verbose = false,
|
||||
.verbose_link = false,
|
||||
.build_mode = builtin.Mode.Debug,
|
||||
.static = static,
|
||||
.kind = kind,
|
||||
@ -988,7 +988,7 @@ pub const LibExeObjStep = struct {
|
||||
.linker_script = null,
|
||||
|
||||
.root_src = undefined,
|
||||
.verbose = undefined,
|
||||
.verbose_link = false,
|
||||
.output_h_path = undefined,
|
||||
.out_h_filename = undefined,
|
||||
.assembly_files = undefined,
|
||||
@ -1087,8 +1087,8 @@ pub const LibExeObjStep = struct {
|
||||
%%self.source_files.append(file);
|
||||
}
|
||||
|
||||
pub fn setVerbose(self: &LibExeObjStep, value: bool) {
|
||||
self.verbose = value;
|
||||
pub fn setVerboseLink(self: &LibExeObjStep, value: bool) {
|
||||
self.verbose_link = value;
|
||||
}
|
||||
|
||||
pub fn setBuildMode(self: &LibExeObjStep, mode: builtin.Mode) {
|
||||
@ -1223,15 +1223,12 @@ pub const LibExeObjStep = struct {
|
||||
%%zig_args.append(builder.pathFromRoot(asm_file));
|
||||
}
|
||||
|
||||
if (self.verbose) {
|
||||
%%zig_args.append("--verbose");
|
||||
}
|
||||
if (builder.verbose_tokenize) %%zig_args.append("--verbose-tokenize");
|
||||
if (builder.verbose_ast) %%zig_args.append("--verbose-ast");
|
||||
if (builder.verbose_cimport) %%zig_args.append("--verbose-cimport");
|
||||
if (builder.verbose_ir) %%zig_args.append("--verbose-ir");
|
||||
if (builder.verbose_llvm_ir) %%zig_args.append("--verbose-llvm-ir");
|
||||
if (builder.verbose_link) %%zig_args.append("--verbose-link");
|
||||
if (builder.verbose_link or self.verbose_link) %%zig_args.append("--verbose-link");
|
||||
|
||||
if (self.strip) {
|
||||
%%zig_args.append("--strip");
|
||||
|
@ -1,4 +1,6 @@
|
||||
extern "c" fn __error() -> &c_int;
|
||||
pub extern "c" fn _NSGetExecutablePath(buf: &u8, bufsize: &u32) -> c_int;
|
||||
|
||||
|
||||
pub use @import("../os/darwin_errno.zig");
|
||||
|
||||
|
@ -513,7 +513,7 @@ pub fn readFileAllocExtra(path: []const u8, allocator: &mem.Allocator, extra_len
|
||||
%defer allocator.free(buf);
|
||||
|
||||
var adapter = FileInStream.init(&file);
|
||||
%return adapter.stream.readNoEof(buf);
|
||||
%return adapter.stream.readNoEof(buf[0..size]);
|
||||
return buf;
|
||||
}
|
||||
|
||||
|
@ -15,6 +15,7 @@ const LinkedList = std.LinkedList;
|
||||
|
||||
error PermissionDenied;
|
||||
error ProcessNotFound;
|
||||
error InvalidName;
|
||||
|
||||
var children_nodes = LinkedList(&ChildProcess).init();
|
||||
|
||||
@ -643,6 +644,7 @@ fn windowsCreateProcess(app_name: &u8, cmd_line: &u8, envp_ptr: ?&u8, cwd_ptr: ?
|
||||
return switch (err) {
|
||||
windows.ERROR.FILE_NOT_FOUND, windows.ERROR.PATH_NOT_FOUND => error.FileNotFound,
|
||||
windows.ERROR.INVALID_PARAMETER => unreachable,
|
||||
windows.ERROR.INVALID_NAME => error.InvalidName,
|
||||
else => os.unexpectedErrorWindows(err),
|
||||
};
|
||||
}
|
||||
|
@ -1516,8 +1516,8 @@ const unexpected_error_tracing = false;
|
||||
/// and you get an unexpected error.
|
||||
pub fn unexpectedErrorPosix(errno: usize) -> error {
|
||||
if (unexpected_error_tracing) {
|
||||
io.stderr.printf("unexpected errno: {}\n", errno) %% return error.Unexpected;
|
||||
debug.printStackTrace() %% return error.Unexpected;
|
||||
debug.warn("unexpected errno: {}\n", errno);
|
||||
debug.dumpStackTrace();
|
||||
}
|
||||
return error.Unexpected;
|
||||
}
|
||||
@ -1526,8 +1526,8 @@ pub fn unexpectedErrorPosix(errno: usize) -> error {
|
||||
/// and you get an unexpected error.
|
||||
pub fn unexpectedErrorWindows(err: windows.DWORD) -> error {
|
||||
if (unexpected_error_tracing) {
|
||||
io.stderr.printf("unexpected GetLastError(): {}\n", err) %% return error.Unexpected;
|
||||
debug.printStackTrace() %% return error.Unexpected;
|
||||
debug.warn("unexpected GetLastError(): {}\n", err);
|
||||
debug.dumpStackTrace();
|
||||
}
|
||||
return error.Unexpected;
|
||||
}
|
||||
@ -1544,6 +1544,53 @@ pub fn openSelfExe() -> %io.File {
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the path to the current executable.
|
||||
/// If you only need the directory, use selfExeDirPath.
|
||||
/// If you only want an open file handle, use openSelfExe.
|
||||
/// This function may return an error if the current executable
|
||||
/// was deleted after spawning.
|
||||
/// Caller owns returned memory.
|
||||
pub fn selfExePath(allocator: &mem.Allocator) -> %[]u8 {
|
||||
switch (builtin.os) {
|
||||
Os.linux => {
|
||||
// If the currently executing binary has been deleted,
|
||||
// the file path looks something like `/a/b/c/exe (deleted)`
|
||||
return readLink(allocator, "/proc/self/exe");
|
||||
},
|
||||
Os.windows => {
|
||||
var out_path = %return Buffer.initSize(allocator, 0xff);
|
||||
%defer out_path.deinit();
|
||||
while (true) {
|
||||
const dword_len = %return math.cast(windows.DWORD, out_path.len());
|
||||
const copied_amt = windows.GetModuleFileNameA(null, out_path.ptr(), dword_len);
|
||||
if (copied_amt <= 0) {
|
||||
const err = windows.GetLastError();
|
||||
return switch (err) {
|
||||
else => unexpectedErrorWindows(err),
|
||||
};
|
||||
}
|
||||
if (copied_amt < out_path.len()) {
|
||||
out_path.shrink(copied_amt);
|
||||
return out_path.toOwnedSlice();
|
||||
}
|
||||
const new_len = (out_path.len() << 1) | 0b1;
|
||||
%return out_path.resize(new_len);
|
||||
}
|
||||
},
|
||||
Os.darwin, Os.macosx, Os.ios => {
|
||||
var u32_len: u32 = 0;
|
||||
const ret1 = c._NSGetExecutablePath(undefined, &u32_len);
|
||||
assert(ret1 != 0);
|
||||
const bytes = %return allocator.alloc(u8, u32_len);
|
||||
%defer allocator.free(bytes);
|
||||
const ret2 = c._NSGetExecutablePath(bytes.ptr, &u32_len);
|
||||
assert(ret2 == 0);
|
||||
return bytes;
|
||||
},
|
||||
else => @compileError("Unsupported OS"),
|
||||
}
|
||||
}
|
||||
|
||||
/// Get the directory path that contains the current executable.
|
||||
/// Caller owns returned memory.
|
||||
pub fn selfExeDirPath(allocator: &mem.Allocator) -> %[]u8 {
|
||||
@ -1558,20 +1605,11 @@ pub fn selfExeDirPath(allocator: &mem.Allocator) -> %[]u8 {
|
||||
const dir = path.dirname(full_exe_path);
|
||||
return allocator.shrink(u8, full_exe_path, dir.len);
|
||||
},
|
||||
Os.windows => {
|
||||
@panic("TODO windows std.os.selfExeDirPath");
|
||||
//buf_resize(out_path, 256);
|
||||
//for (;;) {
|
||||
// DWORD copied_amt = GetModuleFileName(nullptr, buf_ptr(out_path), buf_len(out_path));
|
||||
// if (copied_amt <= 0) {
|
||||
// return ErrorFileNotFound;
|
||||
// }
|
||||
// if (copied_amt < buf_len(out_path)) {
|
||||
// buf_resize(out_path, copied_amt);
|
||||
// return 0;
|
||||
// }
|
||||
// buf_resize(out_path, buf_len(out_path) * 2);
|
||||
//}
|
||||
Os.windows, Os.darwin, Os.macosx, Os.ios => {
|
||||
const self_exe_path = %return selfExePath(allocator);
|
||||
%defer allocator.free(self_exe_path);
|
||||
const dirname = os.path.dirname(self_exe_path);
|
||||
return allocator.shrink(u8, self_exe_path, dirname.len);
|
||||
},
|
||||
else => @compileError("unimplemented: std.os.selfExeDirPath for " ++ @tagName(builtin.os)),
|
||||
}
|
||||
|
@ -48,6 +48,8 @@ pub extern "kernel32" stdcallcc fn GetExitCodeProcess(hProcess: HANDLE, lpExitCo
|
||||
|
||||
pub extern "kernel32" stdcallcc fn GetFileSizeEx(hFile: HANDLE, lpFileSize: &LARGE_INTEGER) -> BOOL;
|
||||
|
||||
pub extern "kernel32" stdcallcc fn GetModuleFileNameA(hModule: ?HMODULE, lpFilename: LPSTR, nSize: DWORD) -> DWORD;
|
||||
|
||||
pub extern "kernel32" stdcallcc fn GetLastError() -> DWORD;
|
||||
|
||||
pub extern "kernel32" stdcallcc fn GetFileInformationByHandleEx(in_hFile: HANDLE,
|
||||
|
@ -14,7 +14,7 @@ pub fn main() -> %void {
|
||||
var arg_it = os.args();
|
||||
|
||||
// TODO use a more general purpose allocator here
|
||||
var inc_allocator = %%std.heap.IncrementingAllocator.init(30 * 1024 * 1024);
|
||||
var inc_allocator = %%std.heap.IncrementingAllocator.init(40 * 1024 * 1024);
|
||||
defer inc_allocator.deinit();
|
||||
|
||||
const allocator = &inc_allocator.allocator;
|
||||
|
Loading…
Reference in New Issue
Block a user