Last active
March 29, 2024 20:11
-
-
Save 13m0n4de/84912522cce6db31da069baf1add04f8 to your computer and use it in GitHub Desktop.
TCC Patch Enabling File Inclusion via HTTPS using CURL, Modified from @rexim's Patch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
From e312c2984445891e2961384bb98e31ddc4816027 Mon Sep 17 00:00:00 2001 | |
From: 13m0n4de <[email protected]> | |
Date: Sat, 17 Feb 2024 22:39:17 +0800 | |
Subject: [PATCH] Make it possible to include files over https | |
The original patch no longer works on the latest TCC, so I created this one. | |
Additionally, I modified it to download the header files to the same directory as the source code. | |
--- | |
Makefile | 2 +- | |
tcc.c | 7 +++++++ | |
tccpp.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ | |
3 files changed, 60 insertions(+), 1 deletion(-) | |
diff --git a/Makefile b/Makefile | |
index 98f01e49..36f0c030 100644 | |
--- a/Makefile | |
+++ b/Makefile | |
@@ -39,7 +39,7 @@ ifdef CONFIG_WIN32 | |
NATIVE_TARGET = $(ARCH)-win$(if $(findstring arm,$(ARCH)),ce,32) | |
else | |
CFG = -unx | |
- LIBS+=-lm | |
+ LIBS+=-lm -lcurl | |
ifneq ($(CONFIG_ldl),no) | |
LIBS+=-ldl | |
endif | |
diff --git a/tcc.c b/tcc.c | |
index f5cd1d8e..7bac6fe4 100644 | |
--- a/tcc.c | |
+++ b/tcc.c | |
@@ -19,6 +19,7 @@ | |
*/ | |
#include "tcc.h" | |
+#include <curl/curl.h> | |
#if ONE_SOURCE | |
# include "libtcc.c" | |
#endif | |
@@ -291,6 +292,10 @@ redo: | |
if (opt < 0) | |
return 1; | |
+ if (curl_global_init(CURL_GLOBAL_DEFAULT) != 0) { | |
+ tcc_error_noabort("Could not initialize Global cURL context. (Yes, your C compiler is using curl Kappa)"); | |
+ } | |
+ | |
if (n == 0) { | |
if (opt == OPT_HELP) { | |
fputs(help, stdout); | |
@@ -399,6 +404,8 @@ redo: | |
} | |
} | |
+ curl_global_cleanup(); | |
+ | |
if (done && 0 == t && 0 == ret && s->do_bench) | |
tcc_print_stats(s, end_time - start_time); | |
diff --git a/tccpp.c b/tccpp.c | |
index 5a09a27e..9fbfb725 100644 | |
--- a/tccpp.c | |
+++ b/tccpp.c | |
@@ -18,6 +18,7 @@ | |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | |
*/ | |
+#include <curl/curl.h> | |
#define USING_GLOBALS | |
#include "tcc.h" | |
@@ -1409,6 +1410,57 @@ static int parse_include(TCCState *s1, int do_next, int test) | |
#endif | |
return 1; | |
} | |
+ | |
+ // Hacked by https://github.com/13m0n4de | |
+ // Original patch: https://gist.github.com/rexim/a6636976d12f67ea530ece118a700317 | |
+ // Available to everyone under Public Domain | |
+ if (strncmp("https://", name, 8) == 0) { | |
+ CURLU *url = curl_url(); | |
+ if (curl_url_set(url, CURLUPART_URL, name, 0)) { | |
+ tcc_error("Could not parse the include URL '%s'", name); | |
+ } | |
+ | |
+ char *path; | |
+ if (curl_url_get(url, CURLUPART_PATH, &path, 0)) { | |
+ tcc_error("Could not extract path from the include URL '%s'", name); | |
+ } | |
+ | |
+ curl_url_cleanup(url); | |
+ | |
+ // Download to the same directory as the source file | |
+ // ```c title='main.c' | |
+ // #include <https://example.com/header.h> | |
+ // ``` | |
+ // After running `tcc /path/to/source/main.c`, | |
+ // the header file will be downloaded as `/path/to/source/header.h` | |
+ p = file->true_filename; // "/path/to/source/main.c" | |
+ pstrncpy(buf, p, tcc_basename(p) - p); // "/path/to/source/" | |
+ pstrcat(buf, sizeof buf, tcc_basename(path)); // "/path/to/source/header.h" | |
+ | |
+ curl_free(path); | |
+ | |
+ CURL *curl = curl_easy_init(); | |
+ if (curl == NULL) { | |
+ tcc_error("Could not initialize cURL context"); | |
+ } | |
+ | |
+ FILE *download_file = fopen(buf, "w"); | |
+ if (download_file == NULL) { | |
+ tcc_error("Could not open file '%s': %s", buf, strerror(errno)); | |
+ } | |
+ | |
+ curl_easy_setopt(curl, CURLOPT_URL, name); | |
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, download_file); | |
+ CURLcode res = curl_easy_perform(curl); | |
+ if (res != CURLE_OK) { | |
+ tcc_error("Could not download '%s': %s", name, curl_easy_strerror(res)); | |
+ } | |
+ | |
+ curl_easy_cleanup(curl); | |
+ fclose(download_file); | |
+ } | |
+ | |
+ | |
if (tcc_open(s1, buf) >= 0) | |
break; | |
} | |
-- | |
2.43.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It should be able to work at least on the latest repository (for now): https://github.com/TinyCC/tinycc/tree/7d1bbc80d4978c128b8ebead42485d7a79624dcd
Original Patch: https://gist.github.com/rexim/a6636976d12f67ea530ece118a700317
Video: https://www.youtube.com/watch?v=4vSyqK3SK-0