Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add building lcc as an APE executable #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions lcc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.vscode
knob
24 changes: 24 additions & 0 deletions lcc/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
To build LCC as an [APE executable](https://justine.lol/ape.html) that can be run on pretty much all the OS's, follow these steps:


Download cosmocc toolchain and unzip it in cosmocc dir:

```sh
cd cosmocc
wget https://cosmo.zip/pub/cosmocc/cosmocc.zip
unzip cosmocc.zip
```

Then compile the "build script" and run it:
> Sidenote: You can actually debug the "build script" if you pass `-ggdb3` to the command
```sh
cc -o knob knob.c
./knob
```

You should then have in the build directory:
- q3rcc.com
- q3cpp.com
- lcc.com

These binaries can run on Windows,Mac OS X, Linux, FreeBSD, OpenBSD, etc.
8 changes: 8 additions & 0 deletions lcc/cosmocc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Run these commands in this directory:
# wget https://cosmo.zip/pub/cosmocc/cosmocc.zip
# unzip cosmocc.zip

# Ignore everything in this directory
*
# Except this file
!.gitignore
2 changes: 1 addition & 1 deletion lcc/cpp/unix.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ char *basepath( char *fname )
all and others do a terrible job (like calling malloc) */
// -- ouch, that hurts -- ln
/* always use the system memmove() on Mac OS X. --ryan. */
#if !defined(__APPLE__) && !defined(_MSC_VER)
#if !defined(__APPLE__) && !defined(_MSC_VER) && !defined(COSMOPOLITAN)
#ifdef memmove
#undef memmove
#endif
Expand Down
3 changes: 3 additions & 0 deletions lcc/etc/bytecodelcc.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
#ifdef _WIN32
#define BINEXT ".exe"
#define PATH_SEP '\\'
#elif defined(COSMOPOLITAN)
#define BINEXT ".com"
#define PATH_SEP '/'
#else
#define BINEXT ""
#define PATH_SEP '/'
Expand Down
69 changes: 69 additions & 0 deletions lcc/knob.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#define KNOB_IMPLEMENTATION
#include "knob.h"


MAIN(lcc_build){

const char* program = knob_shift_args(&argc,&argv);
char* subcommand = NULL;
if(argc <= 0){
subcommand = "build";
} else {
subcommand = (char*)knob_shift_args(&argc,&argv);
}

char cwd[260] = {0};
getcwd(cwd,260);
compiler_names[COMPILER_COSMOCC][0] = knob_temp_sprintf("%s/cosmocc/bin/cosmocc",cwd);
#define NUM_EXE 3
const char* src_dir[NUM_EXE] = {"./src","./cpp","./etc"};
const char* exe_name[NUM_EXE] = {"q3rcc.com","q3cpp.com","lcc.com"};
for(int i =0; i < NUM_EXE;++i){
Knob_Cmd cmd = {0};
Knob_Config config = {0};
knob_config_init(&config);
config.compiler = COMPILER_COSMOCC;

Knob_File_Paths files = {0};
knob_da_mult_append(&files,
src_dir[i]
);
config.build_to = "."PATH_SEP"build";
knob_config_add_files(&config,files.items,files.count);
files.count = 0;
knob_da_mult_append(&files,
src_dir[i]
);
knob_config_add_includes(&config,files.items,files.count);
knob_config_add_c_flag(&config,"-fno-strict-aliasing");
knob_config_add_c_flag(&config,"-fmessage-length=0");
knob_config_add_c_flag(&config,"-fno-common");
knob_config_add_c_flag(&config,"-Wall");
knob_config_add_c_flag(&config,"-Wno-misleading-indentation");
knob_config_add_c_flag(&config,"-O0");
knob_config_add_define(&config,"-DCOSMOPOLITAN");


Knob_File_Paths out_files = {0};
if(!knob_config_build(&config,&out_files,0))return 1;


cmd.count = 0;

knob_cmd_add_compiler(&cmd,&config);
knob_cmd_add_includes(&cmd,&config);
for(int i =0; i < out_files.count;++i){
knob_cmd_append(&cmd,out_files.items[i]);
}

knob_cmd_append(&cmd,"-o",knob_temp_sprintf("./build/%s",exe_name[i]));
knob_cmd_append(&cmd,"-lm");

Knob_String_Builder render = {0};
knob_cmd_render(cmd,&render);
knob_log(KNOB_INFO,"CMD: %s",render.items);
if(!knob_cmd_run_sync(cmd)) return 1;
}

return 0;
}
Loading