public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
From: Johnny Richard <johnny@johnnyrichard.com>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Johnny Richard <johnny@johnnyrichard.com>
Subject: [PATCH olang v1 2/3] cli: add option -c to compile without linking
Date: Thu, 17 Oct 2024 02:19:59 +0200	[thread overview]
Message-ID: <20241017002102.454851-3-johnny@johnnyrichard.com> (raw)
In-Reply-To: <20241017002102.454851-1-johnny@johnnyrichard.com>

Signed-off-by: Johnny Richard <johnny@johnnyrichard.com>
---
 docs/man/man1/olc.1        |  6 ++++++
 src/cli.c                  |  3 +++
 src/cli.h                  |  3 ++-
 src/codegen_linux_x86_64.c |  6 ------
 src/main.c                 | 23 +++++++++++++----------
 5 files changed, 24 insertions(+), 17 deletions(-)

diff --git a/docs/man/man1/olc.1 b/docs/man/man1/olc.1
index 5b35f05..c44df5e 100644
--- a/docs/man/man1/olc.1
+++ b/docs/man/man1/olc.1
@@ -33,6 +33,12 @@ Display AST tree to stdout right after syntax analyzes
 .BI \-o\  file
 Compile program into a binary file
 
+.TP
+.BI \-c
+Compile or assemble the source files, but do not link.  The linking stage
+simply  is  not  done.   The ultimate output is in the form of an object file
+for each source file.
+
 .TP
 .BR \-\-save\-temps
 Keep temp files used to compile program
diff --git a/src/cli.c b/src/cli.c
index 2f61acb..afed19b 100644
--- a/src/cli.c
+++ b/src/cli.c
@@ -53,6 +53,8 @@ cli_parse_args(int argc, char **argv)
             opts.options |= CLI_OPT_SAVE_TEMPS;
         } else if (strcmp(arg, "-o") == 0) {
             cli_opts_parse_output(&opts, &args);
+        } else if (strcmp(arg, "-c") == 0) {
+            opts.options |= CLI_OPT_COMPILE_ONLY;
         } else if (strcmp(arg, "--arch") == 0) {
             opts.options |= CLI_OPT_ARCH;
             cli_opts_parse_arch(&opts, &args);
@@ -153,6 +155,7 @@ cli_print_usage(FILE *stream, char *compiler_path)
         "  --sysroot <dir>  System root dir where the GNU Assembler and GNU "
         "Linker are located: default to '/'\n"
         "  -o <file>        Compile program into a binary file\n"
+        "  -c               Assemble the source files, but do not link\n"
         "  --save-temps     Keep temp files used to compile program\n",
         compiler_path);
 }
diff --git a/src/cli.h b/src/cli.h
index 1a93443..ee02adf 100644
--- a/src/cli.h
+++ b/src/cli.h
@@ -43,7 +43,8 @@ typedef enum
     CLI_OPT_SAVE_TEMPS = 1 << 2,
     CLI_OPT_ARCH = 1 << 3,
     CLI_OPT_SYSROOT = 1 << 4,
-    CLI_OPT_DUMP_AST = 1 << 5
+    CLI_OPT_DUMP_AST = 1 << 5,
+    CLI_OPT_COMPILE_ONLY = 1 << 6
 } cli_opt_t;
 
 cli_opts_t
diff --git a/src/codegen_linux_x86_64.c b/src/codegen_linux_x86_64.c
index 6d38b53..6348dad 100644
--- a/src/codegen_linux_x86_64.c
+++ b/src/codegen_linux_x86_64.c
@@ -107,24 +107,18 @@ codegen_linux_x86_64_emit_translation_unit(codegen_x86_64_t *codegen,
 
     list_item_t *item = list_head(translation_unit.decls);
 
-    bool main_found = false;
-
     while (item != NULL) {
         ast_node_t *decl = (ast_node_t *)item->value;
 
         if (decl->kind == AST_NODE_FN_DEF) {
             ast_fn_definition_t fn = decl->as_fn_def;
             codegen_linux_x86_64_emit_function(codegen, &fn);
-
-            main_found = main_found || string_view_eq_to_cstr(fn.id, "main");
         } else {
             assert(0 && "translation unit only supports function declarations");
         }
 
         item = list_next(item);
     }
-
-    assert(main_found && "main function is required.");
 }
 
 static size_t
diff --git a/src/main.c b/src/main.c
index c68fd48..49b2240 100644
--- a/src/main.c
+++ b/src/main.c
@@ -173,10 +173,11 @@ handle_codegen_linux(cli_opts_t *opts)
 
     char command[512];
     sprintf(command,
-            "%s/bin/as %s -o " SV_FMT ".o",
+            "%s/bin/as %s -o " SV_FMT "%s",
             opts->sysroot,
             asm_file,
-            SV_ARG(opts->output_bin));
+            SV_ARG(opts->output_bin),
+            opts->options & CLI_OPT_COMPILE_ONLY ? "" : ".o");
 
     int exit_code = system(command);
 
@@ -184,16 +185,18 @@ handle_codegen_linux(cli_opts_t *opts)
         exit(exit_code);
     }
 
-    sprintf(command,
-            "%s/bin/cc " SV_FMT ".o -o " SV_FMT,
-            opts->sysroot,
-            SV_ARG(opts->output_bin),
-            SV_ARG(opts->output_bin));
+    if (!(opts->options & CLI_OPT_COMPILE_ONLY)) {
+        sprintf(command,
+                "%s/bin/cc " SV_FMT ".o -o " SV_FMT,
+                opts->sysroot,
+                SV_ARG(opts->output_bin),
+                SV_ARG(opts->output_bin));
 
-    exit_code = system(command);
+        exit_code = system(command);
 
-    if (exit_code != 0) {
-        exit(exit_code);
+        if (exit_code != 0) {
+            exit(exit_code);
+        }
     }
 
     if (!(opts->options & CLI_OPT_SAVE_TEMPS)) {
-- 
2.46.0


  parent reply	other threads:[~2024-10-16 22:21 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-17  0:19 [PATCH olang v1 0/3] cli: enable object file binary compilation Johnny Richard
2024-10-16 22:25 ` Carlos Maniero
2024-10-17  0:19 ` [PATCH olang v1 1/3] codegen: link olang binary with libc by default Johnny Richard
2024-10-17  0:19 ` Johnny Richard [this message]
2024-10-17  0:20 ` [PATCH olang v1 3/3] codegen: remove linux from codegen namespace Johnny Richard
2024-10-16 22:22   ` [olang/patches/.build.yml] build success builds.sr.ht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241017002102.454851-3-johnny@johnnyrichard.com \
    --to=johnny@johnnyrichard.com \
    --cc=~johnnyrichard/olang-devel@lists.sr.ht \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.johnnyrichard.com/olang.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox