essence-os/util/build_gcc.script

187 lines
8.4 KiB
Plaintext

#import "util/get_source.script" get_source;
// Persistent variables:
bool runningMakefiles #persist;
// Options:
str gccVersion #option;
str binutilsVersion #option;
str targetName #option;
str toolchainPrefix #option;
int processorCount #option;
bool skipYesChecks #option;
bool ConsoleGetYes() {
if skipYesChecks { return true; }
str input = ConsoleGetLine();
return input == "yes" || input == "y";
}
void Start() {
// Set default values for options.
if gccVersion == "" gccVersion = "11.1.0";
if binutilsVersion == "" binutilsVersion = "2.36.1";
if targetName == "" targetName = "x86_64";
if toolchainPrefix == "" toolchainPrefix = "x86_64-essence";
// Get the number of processors to compile with.
if processorCount == 0 {
processorCount = SystemGetProcessorCount();
if processorCount < 1 processorCount = 1;
if processorCount > 16 processorCount = 16;
}
// Print instructions.
PrintStdErr("To build Essence, you need a cross compiler. This will be built for you automatically.\n");
PrintStdErr("- You need to be connected to the internet. ~100MB will be downloaded.\n");
PrintStdErr("- You need ~3GB of drive space available.\n");
PrintStdErr("- You need ~8GB of RAM available.\n");
PrintStdErr("- This should take ~20 minutes on a modern computer.\n");
PrintStdErr("- This does *not* require root permissions.\n");
PrintStdErr("- You must fully update your system before building.\n");
PrintStdErr("Enter 'yes' to continue.\n");
assert ConsoleGetYes();
// Create folders.
assert PathCreateDirectory("bin");
assert PathCreateDirectory("root");
assert PathCreateDirectory("root/Essence");
assert PathCreateDirectory("root/Applications");
assert PathCreateDirectory("root/Applications/POSIX");
assert PathCreateDirectory("root/Applications/POSIX/bin");
assert PathCreateDirectory("root/Applications/POSIX/include");
assert PathCreateDirectory("root/Applications/POSIX/lib");
// Load the persistent variables.
assert PersistRead("bin/build_gcc_state.dat");
// Get the directories.
str sourceTree = PathGetDefaultPrefix();
str crossDirectory = sourceTree + "/cross";
str rootDirectory = sourceTree + "/root";
str compilerPath = crossDirectory + "/bin";
// Modify the path.
str path = compilerPath + ":" + SystemGetEnvironmentVariable("PATH");
SystemSetEnvironmentVariable("PATH", path);
assert !StringContains(path, "::");
assert SystemGetEnvironmentVariable("PATH") == path;
// Get the brew library path if we're running on Darwin.
str hostPlatform = StringTrim(SystemShellEvaluate("uname"));
str libraryPath = "";
if hostPlatform == "Darwin" libraryPath = SystemShellEvaluate("brew --prefix") + "/lib";
// Make sure we're not running as root.
assert StringTrim(SystemShellEvaluate("whoami")) != "root";
// Check all the needed tools are available.
assert SystemShellExecute("which g++");
assert SystemShellExecute("which make");
assert SystemShellExecute("which bison");
assert SystemShellExecute("which flex");
assert SystemShellExecute("which curl");
assert SystemShellExecute("which nasm");
assert SystemShellExecute("which ctags");
assert SystemShellExecute("which xz");
assert SystemShellExecute("which gzip");
assert SystemShellExecute("which tar");
assert SystemShellExecute("which grep");
assert SystemShellExecute("which sed");
assert SystemShellExecute("which awk");
// Check all the needed libraries are available.
assert FileWriteAll("bin/test.c", "void main() {}");
assert !SystemShellExecute("gcc %libraryPath% -lmpc bin/test.c 2>&1 | grep -i undefined"); // libmpc-dev
assert !SystemShellExecute("gcc %libraryPath% -lmpfr bin/test.c 2>&1 | grep -i undefined"); // libmpfr-dev
assert !SystemShellExecute("gcc %libraryPath% -lgmp bin/test.c 2>&1 | grep -i undefined"); // libgmp-dev
// Ask the user if they want to resume an incomplete build.
bool resumeBuild = false;
if runningMakefiles {
PrintStdErr("The build system has detected a build was started, but was not completed.\n");
PrintStdErr("Enter 'yes' to attempt to resume this build.\n");
resumeBuild = ConsoleGetYes();
runningMakefiles = false;
}
if !resumeBuild {
// Cleanup.
assert PathDeleteRecursively("cross");
assert PathDeleteRecursively("bin/build-binutils");
assert PathDeleteRecursively("bin/build-gcc");
assert PathDeleteRecursively("bin/binutils-src");
assert PathDeleteRecursively("bin/gcc-src");
// Create folders.
assert PathCreateDirectory("bin/build-binutils");
assert PathCreateDirectory("bin/build-gcc");
// Copy the C standard library headers to their destination.
assert SystemShellExecute("ports/musl/build.sh %targetName%");
// Download the sources.
get_source.Get("ftp://ftp.gnu.org/gnu/binutils/binutils-%binutilsVersion%.tar.xz", "binutils-%binutilsVersion%",
"e81d9edf373f193af428a0f256674aea62a9d74dfe93f65192d4eae030b0f3b0");
assert PathMove("bin/source", "bin/binutils-src");
get_source.Get("ftp://ftp.gnu.org/gnu/gcc/gcc-%gccVersion%/gcc-%gccVersion%.tar.xz", "gcc-%gccVersion%",
"4c4a6fb8a8396059241c2e674b85b351c26a5d678274007f076957afa1cc9ddf");
assert PathMove("bin/source", "bin/gcc-src");
// Patch the sources.
assert FileCopy("ports/gcc/changes/binutils_bfd_config.bfd", "bin/binutils-src/bfd/config.bfd");
assert FileCopy("ports/gcc/changes/binutils_config.sub", "bin/binutils-src/config.sub");
assert FileCopy("ports/gcc/changes/binutils_gas_configure.tgt", "bin/binutils-src/gas/configure.tgt");
assert FileCopy("ports/gcc/changes/binutils_ld_configure.tgt", "bin/binutils-src/ld/configure.tgt");
assert FileCopy("ports/gcc/changes/gcc_config.sub", "bin/gcc-src/config.sub");
assert FileCopy("ports/gcc/changes/gcc_fixincludes_mkfixinc.sh", "bin/gcc-src/fixincludes/mkfixinc.sh");
assert FileCopy("ports/gcc/changes/gcc_gcc_config_essence.h", "bin/gcc-src/gcc/config/essence.h");
assert FileCopy("ports/gcc/changes/gcc_gcc_config_i386_t-x86_64-essence", "bin/gcc-src/gcc/config/i386/t-x86_64-essence");
assert FileCopy("ports/gcc/changes/gcc_gcc_config.gcc", "bin/gcc-src/gcc/config.gcc");
assert FileCopy("ports/gcc/changes/gcc_gcc_config_host_darwin.c", "bin/gcc-src/gcc/config/host-darwin.c");
assert FileCopy("ports/gcc/changes/gcc_libgcc_config.host", "bin/gcc-src/libgcc/config.host");
assert FileCopy("ports/gcc/changes/gcc_libstdc++-v3_configure", "bin/gcc-src/libstdc++-v3/configure");
// Run configure.
assert SystemShellExecuteWithWorkingDirectory("bin/build-binutils",
"../binutils-src/configure --target=%toolchainPrefix% --prefix=\"%crossDirectory%\" "
+ "--with-sysroot=\"%rootDirectory%\" --disable-nls --disable-werror");
assert SystemShellExecuteWithWorkingDirectory("bin/build-gcc",
"../gcc-src/configure --target=%toolchainPrefix% --prefix=\"%crossDirectory%\" "
+ "--enable-languages=c,c++ --with-sysroot=\"%rootDirectory%\" --disable-nls");
// Add --without-headers for a x86_64-elf build.
}
// Run makefiles.
runningMakefiles = true;
assert SystemShellExecuteWithWorkingDirectory("bin/build-binutils", "make -j %processorCount%");
assert SystemShellExecuteWithWorkingDirectory("bin/build-binutils", "make install");
assert SystemShellExecuteWithWorkingDirectory("bin/build-gcc", "make all-gcc -j %processorCount%");
assert SystemShellExecuteWithWorkingDirectory("bin/build-gcc", "make all-target-libgcc -j %processorCount%");
assert SystemShellExecuteWithWorkingDirectory("bin/build-gcc", "make install-gcc");
assert SystemShellExecuteWithWorkingDirectory("bin/build-gcc", "make install-target-libgcc");
// Remove debugging symbols.
SystemShellExecute("strip %crossDirectory%/bin/*");
SystemShellExecute("strip %crossDirectory%/libexec/gcc/%toolchainPrefix%/%gccVersion%/*");
// Modify the mm_malloc.h header.
assert FileWriteAll("%crossDirectory%/lib/gcc/%toolchainPrefix%/%gccVersion%/include/mm_malloc.h", "/* removed */\n");
// Update the build configuration and compile the system.
FileWriteAll("bin/build_config.ini", "accepted_license=1\ncompiler_path=%compilerPath%\ncross_compiler_index=11\n");
SystemShellExecute("./start.sh c");
// Build libstdc++.
assert SystemShellExecuteWithWorkingDirectory("bin/build-gcc", "make all-target-libstdc++-v3 -j %processorCount%");
assert SystemShellExecuteWithWorkingDirectory("bin/build-gcc", "make install-target-libstdc++-v3");
// Cleanup.
runningMakefiles = false;
assert PathDeleteRecursively("bin/build-binutils");
assert PathDeleteRecursively("bin/build-gcc");
assert PathDeleteRecursively("bin/binutils-src");
assert PathDeleteRecursively("bin/gcc-src");
PrintStdErrHighlight("Build succeeded.\n");
}