From patchwork Wed Jan 15 20:05:40 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Steadmon X-Patchwork-Id: 13940871 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 947851D61BB for ; Wed, 15 Jan 2025 20:05:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736971551; cv=none; b=il8LhfQytb7ximsfMy13aChCz0dUsytHAlURmdAcK2ZguAWEitIpVit4oIYhrCaJuKJmLQmC4IeiP9OuW4ybf5DBRo303kRN+5yQPrJ1Rj2Aeu4p8dWXPZQ4kRJJOzgzGkEhqIrYRjLJNjpC0z+9g2qgtAgZoGhZupBKR4H2MBk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736971551; c=relaxed/simple; bh=VJnKyKv1fpjrdio38HIZaxetMr/dZzyrE2vO98agGns=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=TvbzKa0gnyE0wtbozDVhKgPIKWI4+vm25UF5ufVb30E+8VO87oFukXbFhOay/yV7rrykA5J3BTsAS6BdAjhMZccYkABBL7jFacVdF0g/KDMMSbrEv4MWhXJRjiiaFm6MEJ/T3SuE+KQIyC1vwtWYhHdwG6lbKaO7cYrUth53/W0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--steadmon.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=yCh+UjS0; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--steadmon.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="yCh+UjS0" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-21638389f63so1073635ad.1 for ; Wed, 15 Jan 2025 12:05:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736971549; x=1737576349; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=lQrEMC+sl7wsF0MXsCGtyp2CMun9dzs/KYxOHQiGEdI=; b=yCh+UjS0g2RFKfzBMthgF7ImWnlm73uk0Ve0K0ivDWMWuAs57ZMSIA5Y0QWgoZoChI oHgfW+TM0Sjinf6weRdnumSvBRZApZ6mOOZDJAB/ltLDgD02TLC0/W2yvzaXL9Zn63fz rYn3iX1AgWF+qEyXWoL24/BqriK1wVHJetpk3IVkRVf4JBXexFL+MMopILMPW73OuWjb 6uMcLgdZ/1vRd7LfmDaAevxVaHvIRDr/968RYEMNnomDkKQf2VuCM9/57EcBVTP+az3q mUXOMpCh2CJC2DfuBctxfAI43LROPLUF1mBRFSYAmrG8sTprK1tjNQsqYDqc3cavl3sQ 33HQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736971549; x=1737576349; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=lQrEMC+sl7wsF0MXsCGtyp2CMun9dzs/KYxOHQiGEdI=; b=hZaciGZzHiuxVz4Eq2p6iBy+MA/r/spEU2qdlyRZZSzapl+B3sgClWhLR0bb15zm6E tiaCRpGmFuQ3ZlDXShoZYMhzGu6dBQjvSEK5tTbGvEnurxuYwlibg7NN0iMQVwibRJXa deH875mjZGknhwWsFxYPBT0ph9gLtWm7N5YbtwgACsYrXRMGEA4fEltbpbqr7/1QIIMY u6r71hnGs+DtOQwD1Erek1kUXQQ3R4jrYyzVai+HDzNZfC8Ys/NqePEezjmGTmuDfmPT TZrKdJkIoe0APDD3BsuLHnlzm5fAhCCf77S5j3/2YEAt8S59Ca7vau8vmv2HAOl/hjbu qnCw== X-Gm-Message-State: AOJu0YwlcbjDo40BsF/eeYtK/XhhTX0SgQEQr3g3oloYR6AYZqa77FOl lY/u9xnQCNpE9hlsd9Z5Zkml1TKiGq9UDe6R9Ge5bgxeYJFeBT+oMWbHJTbO4FSub+FZgkeZlC9 XdHMQWNCVzsIPI3op2aETMThISbOqQJKc0LVodwJkBwR+wAcntMkzfpDlZw6zAYYu0fESHX8/nh bHu57j+7jQRoOTJcrHnqK95/xSDBZ1MXehLZvhb8I= X-Google-Smtp-Source: AGHT+IFO4TIbGpmI8m8x/Jpg3/nin3BGV/iRpsrnnpZu4P6z/2jI+lW1xkau7+YVDp/m5+0fb2VSunMBT+oFbg== X-Received: from pfcj1.prod.google.com ([2002:a05:6a00:1741:b0:724:eefc:69ef]) (user=steadmon job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:841a:b0:1ea:eca7:10ee with SMTP id adf61e73a8af0-1eaeca71173mr19624855637.6.1736971548603; Wed, 15 Jan 2025 12:05:48 -0800 (PST) Date: Wed, 15 Jan 2025 12:05:40 -0800 In-Reply-To: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog Message-ID: Subject: [PATCH v6 1/5] common-main: split init and exit code into new files From: Josh Steadmon To: git@vger.kernel.org Cc: calvinwan@google.com, nasamuffin@google.com, emrass@google.com, gitster@pobox.com, sandals@crustytoothpaste.net, mh@glandium.org, ps@pks.im, sunshine@sunshineco.com, phillip.wood123@gmail.com, allred.sean@gmail.com Currently, object files in libgit.a reference common_exit(), which is contained in common-main.o. However, common-main.o also includes main(), which references cmd_main() in git.o, which in turn depends on all the builtin/*.o objects. We would like to allow external users to link libgit.a without needing to include so many extra objects. Enable this by splitting common_exit() and check_bug_if_BUG() into a new file common-exit.c, and add common-exit.o to LIB_OBJS so that these are included in libgit.a. This split has previously been proposed ([1], [2]) to support fuzz tests and unit tests by avoiding conflicting definitions for main(). However, both of those issues were resolved by other methods of avoiding symbol conflicts. Now we are trying to make libgit.a more self-contained, so hopefully we can revisit this approach. Additionally, move the initialization code out of main() into a new init_git() function in its own file. Include this in libgit.a as well, so that external users can share our setup code without calling our main(). [1] https://lore.kernel.org/git/Yp+wjCPhqieTku3X@google.com/ [2] https://lore.kernel.org/git/20230517-unit-tests-v2-v2-1-21b5b60f4b32@google.com/ Signed-off-by: Josh Steadmon --- Makefile | 2 ++ common-exit.c | 26 ++++++++++++++++ common-init.c | 63 ++++++++++++++++++++++++++++++++++++++ common-init.h | 6 ++++ common-main.c | 83 ++------------------------------------------------- 5 files changed, 99 insertions(+), 81 deletions(-) create mode 100644 common-exit.c create mode 100644 common-init.c create mode 100644 common-init.h diff --git a/Makefile b/Makefile index 97e8385b66..27e68ac039 100644 --- a/Makefile +++ b/Makefile @@ -981,6 +981,8 @@ LIB_OBJS += combine-diff.o LIB_OBJS += commit-graph.o LIB_OBJS += commit-reach.o LIB_OBJS += commit.o +LIB_OBJS += common-exit.o +LIB_OBJS += common-init.o LIB_OBJS += compat/nonblock.o LIB_OBJS += compat/obstack.o LIB_OBJS += compat/terminal.o diff --git a/common-exit.c b/common-exit.c new file mode 100644 index 0000000000..1aaa538be3 --- /dev/null +++ b/common-exit.c @@ -0,0 +1,26 @@ +#include "git-compat-util.h" +#include "trace2.h" + +static void check_bug_if_BUG(void) +{ + if (!bug_called_must_BUG) + return; + BUG("on exit(): had bug() call(s) in this process without explicit BUG_if_bug()"); +} + +/* We wrap exit() to call common_exit() in git-compat-util.h */ +int common_exit(const char *file, int line, int code) +{ + /* + * For non-POSIX systems: Take the lowest 8 bits of the "code" + * to e.g. turn -1 into 255. On a POSIX system this is + * redundant, see exit(3) and wait(2), but as it doesn't harm + * anything there we don't need to guard this with an "ifdef". + */ + code &= 0xff; + + check_bug_if_BUG(); + trace2_cmd_exit_fl(file, line, code); + + return code; +} diff --git a/common-init.c b/common-init.c new file mode 100644 index 0000000000..5cc73f058c --- /dev/null +++ b/common-init.c @@ -0,0 +1,63 @@ +#define USE_THE_REPOSITORY_VARIABLE + +#include "git-compat-util.h" +#include "common-init.h" +#include "exec-cmd.h" +#include "gettext.h" +#include "attr.h" +#include "repository.h" +#include "setup.h" +#include "strbuf.h" +#include "trace2.h" + +/* + * Many parts of Git have subprograms communicate via pipe, expect the + * upstream of a pipe to die with SIGPIPE when the downstream of a + * pipe does not need to read all that is written. Some third-party + * programs that ignore or block SIGPIPE for their own reason forget + * to restore SIGPIPE handling to the default before spawning Git and + * break this carefully orchestrated machinery. + * + * Restore the way SIGPIPE is handled to default, which is what we + * expect. + */ +static void restore_sigpipe_to_default(void) +{ + sigset_t unblock; + + sigemptyset(&unblock); + sigaddset(&unblock, SIGPIPE); + sigprocmask(SIG_UNBLOCK, &unblock, NULL); + signal(SIGPIPE, SIG_DFL); +} + +void init_git(const char **argv) +{ + struct strbuf tmp = STRBUF_INIT; + + trace2_initialize_clock(); + + /* + * Always open file descriptors 0/1/2 to avoid clobbering files + * in die(). It also avoids messing up when the pipes are dup'ed + * onto stdin/stdout/stderr in the child processes we spawn. + */ + sanitize_stdfds(); + restore_sigpipe_to_default(); + + git_resolve_executable_dir(argv[0]); + + setlocale(LC_CTYPE, ""); + git_setup_gettext(); + + initialize_repository(the_repository); + + attr_start(); + + trace2_initialize(); + trace2_cmd_start(argv); + trace2_collect_process_info(TRACE2_PROCESS_INFO_STARTUP); + + if (!strbuf_getcwd(&tmp)) + tmp_original_cwd = strbuf_detach(&tmp, NULL); +} diff --git a/common-init.h b/common-init.h new file mode 100644 index 0000000000..3e6db20cae --- /dev/null +++ b/common-init.h @@ -0,0 +1,6 @@ +#ifndef COMMON_INIT_H +#define COMMON_INIT_H + +void init_git(const char **argv); + +#endif /* COMMON_INIT_H */ diff --git a/common-main.c b/common-main.c index 8e68ac9e42..6b7ab077b0 100644 --- a/common-main.c +++ b/common-main.c @@ -1,92 +1,13 @@ -#define USE_THE_REPOSITORY_VARIABLE - #include "git-compat-util.h" -#include "exec-cmd.h" -#include "gettext.h" -#include "attr.h" -#include "repository.h" -#include "setup.h" -#include "strbuf.h" -#include "trace2.h" - -/* - * Many parts of Git have subprograms communicate via pipe, expect the - * upstream of a pipe to die with SIGPIPE when the downstream of a - * pipe does not need to read all that is written. Some third-party - * programs that ignore or block SIGPIPE for their own reason forget - * to restore SIGPIPE handling to the default before spawning Git and - * break this carefully orchestrated machinery. - * - * Restore the way SIGPIPE is handled to default, which is what we - * expect. - */ -static void restore_sigpipe_to_default(void) -{ - sigset_t unblock; - - sigemptyset(&unblock); - sigaddset(&unblock, SIGPIPE); - sigprocmask(SIG_UNBLOCK, &unblock, NULL); - signal(SIGPIPE, SIG_DFL); -} +#include "common-init.h" int main(int argc, const char **argv) { int result; - struct strbuf tmp = STRBUF_INIT; - - trace2_initialize_clock(); - - /* - * Always open file descriptors 0/1/2 to avoid clobbering files - * in die(). It also avoids messing up when the pipes are dup'ed - * onto stdin/stdout/stderr in the child processes we spawn. - */ - sanitize_stdfds(); - restore_sigpipe_to_default(); - - git_resolve_executable_dir(argv[0]); - - setlocale(LC_CTYPE, ""); - git_setup_gettext(); - - initialize_repository(the_repository); - - attr_start(); - - trace2_initialize(); - trace2_cmd_start(argv); - trace2_collect_process_info(TRACE2_PROCESS_INFO_STARTUP); - - if (!strbuf_getcwd(&tmp)) - tmp_original_cwd = strbuf_detach(&tmp, NULL); + init_git(argv); result = cmd_main(argc, argv); /* Not exit(3), but a wrapper calling our common_exit() */ exit(result); } - -static void check_bug_if_BUG(void) -{ - if (!bug_called_must_BUG) - return; - BUG("on exit(): had bug() call(s) in this process without explicit BUG_if_bug()"); -} - -/* We wrap exit() to call common_exit() in git-compat-util.h */ -int common_exit(const char *file, int line, int code) -{ - /* - * For non-POSIX systems: Take the lowest 8 bits of the "code" - * to e.g. turn -1 into 255. On a POSIX system this is - * redundant, see exit(3) and wait(2), but as it doesn't harm - * anything there we don't need to guard this with an "ifdef". - */ - code &= 0xff; - - check_bug_if_BUG(); - trace2_cmd_exit_fl(file, line, code); - - return code; -} From patchwork Wed Jan 15 20:05:41 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Steadmon X-Patchwork-Id: 13940872 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 717C71D8DE4 for ; Wed, 15 Jan 2025 20:05:51 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736971553; cv=none; b=IQpu8W4bQLyB5W/RRb4pYqJIrSrckIY9OdDoaRemBLsVWFiNXKiNVtuNGMyw46e5Kwoin+CQ2aENFRXKjfXHmygkXWIeCxWlLH7farfng+DFzi4pEbSyd3rFehL2C1zm4EmyDz23G8emV8MplxsFDMLSjkGQLJi8jpHBaif8HcQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736971553; c=relaxed/simple; bh=sGTADG9QTrb+M1YWf4tvEvBckxHxHHl4lg9yd3IOEAI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=HTZD/WAIk3PdctNZkqhd7crqTf1AkSeFNwdwc5lW79Y0nB5xKUDThpy1if8+3xcOv7NHCE9cKt0rBDs4UqCaXtVh2mtL/0Qnpoi7djogZxZVQz2AMf8odPhTaYmvo6EshsvYVOlGeC1l1QaoodCMl9dnfqJiW/XGQwlCxtiqAno= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--steadmon.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=W5/RCyvT; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--steadmon.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="W5/RCyvT" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2ef79d9c692so747577a91.0 for ; Wed, 15 Jan 2025 12:05:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736971551; x=1737576351; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=MRpAiykl0uo6UbbhyapmCVMFDJg4ZKJUM8ctcuv/0TU=; b=W5/RCyvTN3b757jXe34Jn4Ts3LsmNBginpzFZ/g6RZHD2zFk5FFItHMM1tkoe+s3IB hKaXGBrm1dJjTWTEgvUkM9eLtoHFNlyCPVuIWlaHck8TF7pB9nypVMmWKPilUVCYKJoL udZUidgQ7BbQ6CJum9zJmqssRDHLnUso6mcVTJYwqtzdxVzn9AC05YM9MnUjRQ8u7Je/ tp5AhDbAxEfFYm23cCr0ZJjN/pfkxZ1VGsKPDV+bx5oSZ2nQeGHE60uCp0S/5j+/Sv9r id1a7vkxiyiEJV3QJhVGocS4e+1eujlaQ4Xu1Wk+pgxl61vtDJ4ZUX3Q2n7acEWrc+rA 2XVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736971551; x=1737576351; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=MRpAiykl0uo6UbbhyapmCVMFDJg4ZKJUM8ctcuv/0TU=; b=qXtZKNntbUoAu87lYCZDvYOYxnFt3Y9WlmW1Nkglswl08vfK7hTnjx9Zl1b0gJEXuV Y2uvDJbpAYqHUN/v4IrHe8vQ/6aRnRCwTY+AUpfOPO92Weq29Ycv5ZN9De7DsG5Wcrrv juPR9j6r2mxOY7jHQ1hRp2WbDZxRjOHIN0BsJ3pRwHQORsgRIunbjVHCnEmobbP+sxYW qqPCyXnn1HAfXaMjKCW2OZjeDXp/XI+AJs8XxiAjTXSz11FmW3IODITMyb1jxmFQEk2Q Be3ZSU6SzT7PCWhlakv4909HZIEymR74z4dzYrDD5Rr1cdwt8dzyVLZk9GNq7DgxoPq6 fVFg== X-Gm-Message-State: AOJu0YwBP+S1cGnuC1jrR/UyomdH00Uw/bZI8v2sx+7V0ZvHxlBpNJMs hgVT6bpiZosEvxz48meIc0ZOUiMGJM7nOE3lVtiri8Uh7Y2wB14RVZT18Dl/La9hQBNuZDsyqLf 1bnKYelasIj6DfhIb2EB4NLeGv3sBHJbx9AMTfbbvkoz44SNcT3dfNcQPWqw0swxLPA378IOkn4 M0zkHuUHr7EhfBZPwRyoR9iy78g8IOI0EIjZUOATY= X-Google-Smtp-Source: AGHT+IHp6makelyMwZRMw/n+OBQgGjRTo7OP/h3gVFK/XPnugWmNrnKTdBETFgmlrNc9INty3iq8m1WBfpmS0A== X-Received: from pja13.prod.google.com ([2002:a17:90b:548d:b0:2ef:9ef2:8790]) (user=steadmon job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:2747:b0:2f2:a664:df1a with SMTP id 98e67ed59e1d1-2f548e9c9bcmr45775878a91.2.1736971550686; Wed, 15 Jan 2025 12:05:50 -0800 (PST) Date: Wed, 15 Jan 2025 12:05:41 -0800 In-Reply-To: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog Message-ID: <5fc66cdb1628e0c9e420f3f0455779d7471f46ee.1736971328.git.steadmon@google.com> Subject: [PATCH v6 2/5] libgit-sys: introduce Rust wrapper for libgit.a From: Josh Steadmon To: git@vger.kernel.org Cc: calvinwan@google.com, nasamuffin@google.com, emrass@google.com, gitster@pobox.com, sandals@crustytoothpaste.net, mh@glandium.org, ps@pks.im, sunshine@sunshineco.com, phillip.wood123@gmail.com, allred.sean@gmail.com Introduce libgit-sys, a Rust wrapper crate that allows Rust code to call functions in libgit.a. This initial patch defines build rules and an interface that exposes user agent string getter functions as a proof of concept. This library can be tested with `cargo test`. In later commits, a higher-level library containing a more Rust-friendly interface will be added at `contrib/libgit-rs`. Symbols in libgit can collide with symbols from other libraries such as libgit2. We avoid this by first exposing library symbols in public_symbol_export.[ch]. These symbols are prepended with "libgit_" to avoid collisions and set to visible using a visibility pragma. In build.rs, Rust builds contrib/libgit-rs/libgit-sys/libgitpub.a, which also contains libgit.a and other dependent libraries, with -fvisibility=hidden to hide all symbols within those libraries that haven't been exposed with a visibility pragma. Co-authored-by: Kyle Lippincott Co-authored-by: Calvin Wan Signed-off-by: Calvin Wan Signed-off-by: Kyle Lippincott Signed-off-by: Josh Steadmon --- .gitignore | 1 + Makefile | 16 ++++++ contrib/libgit-sys/Cargo.lock | 69 +++++++++++++++++++++++ contrib/libgit-sys/Cargo.toml | 18 ++++++ contrib/libgit-sys/README.md | 4 ++ contrib/libgit-sys/build.rs | 35 ++++++++++++ contrib/libgit-sys/public_symbol_export.c | 21 +++++++ contrib/libgit-sys/public_symbol_export.h | 8 +++ contrib/libgit-sys/src/lib.rs | 46 +++++++++++++++ 9 files changed, 218 insertions(+) create mode 100644 contrib/libgit-sys/Cargo.lock create mode 100644 contrib/libgit-sys/Cargo.toml create mode 100644 contrib/libgit-sys/README.md create mode 100644 contrib/libgit-sys/build.rs create mode 100644 contrib/libgit-sys/public_symbol_export.c create mode 100644 contrib/libgit-sys/public_symbol_export.h create mode 100644 contrib/libgit-sys/src/lib.rs diff --git a/.gitignore b/.gitignore index e82aa19df0..31d7e64287 100644 --- a/.gitignore +++ b/.gitignore @@ -250,3 +250,4 @@ Release/ /git.VC.db *.dSYM /contrib/buildsystems/out +/contrib/libgit-sys/target diff --git a/Makefile b/Makefile index 27e68ac039..47e864a861 100644 --- a/Makefile +++ b/Makefile @@ -657,6 +657,8 @@ CURL_CONFIG = curl-config GCOV = gcov STRIP = strip SPATCH = spatch +LD = ld +OBJCOPY = objcopy export TCL_PATH TCLTK_PATH @@ -2731,6 +2733,7 @@ OBJECTS += $(REFTABLE_OBJS) $(REFTABLE_TEST_OBJS) OBJECTS += $(UNIT_TEST_OBJS) OBJECTS += $(CLAR_TEST_OBJS) OBJECTS += $(patsubst %,$(UNIT_TEST_DIR)/%.o,$(UNIT_TEST_PROGRAMS)) +OBJECTS += contrib/libgit-sys/public_symbol_export.o ifndef NO_CURL OBJECTS += http.o http-walker.o remote-curl.o @@ -3726,6 +3729,10 @@ clean: profile-clean coverage-clean cocciclean $(RM) $(htmldocs).tar.gz $(manpages).tar.gz $(MAKE) -C Documentation/ clean $(RM) Documentation/GIT-EXCLUDED-PROGRAMS + $(RM) -r contrib/libgit-sys/target + $(RM) -r contrib/libgit-sys/partial_symbol_export.o + $(RM) -r contrib/libgit-sys/hidden_symbol_export.o + $(RM) -r contrib/libgit-sys/libgitpub.a ifndef NO_PERL $(RM) -r perl/build/ endif @@ -3887,3 +3894,12 @@ $(CLAR_TEST_PROG): $(UNIT_TEST_DIR)/clar.suite $(CLAR_TEST_OBJS) $(GITLIBS) GIT- build-unit-tests: $(UNIT_TEST_PROGS) $(CLAR_TEST_PROG) unit-tests: $(UNIT_TEST_PROGS) $(CLAR_TEST_PROG) t/helper/test-tool$X $(MAKE) -C t/ unit-tests + +contrib/libgit-sys/partial_symbol_export.o: contrib/libgit-sys/public_symbol_export.o libgit.a reftable/libreftable.a xdiff/lib.a + $(LD) -r $^ -o $@ + +contrib/libgit-sys/hidden_symbol_export.o: contrib/libgit-sys/partial_symbol_export.o + $(OBJCOPY) --localize-hidden $^ $@ + +contrib/libgit-sys/libgitpub.a: contrib/libgit-sys/hidden_symbol_export.o + $(AR) $(ARFLAGS) $@ $^ diff --git a/contrib/libgit-sys/Cargo.lock b/contrib/libgit-sys/Cargo.lock new file mode 100644 index 0000000000..427a4c66b7 --- /dev/null +++ b/contrib/libgit-sys/Cargo.lock @@ -0,0 +1,69 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "autocfg" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" + +[[package]] +name = "cc" +version = "1.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57b6a275aa2903740dc87da01c62040406b8812552e97129a63ea8850a17c6e6" +dependencies = [ + "shlex", +] + +[[package]] +name = "libc" +version = "0.2.158" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439" + +[[package]] +name = "libgit-sys" +version = "0.1.0" +dependencies = [ + "autocfg", + "libz-sys", + "make-cmd", +] + +[[package]] +name = "libz-sys" +version = "1.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2d16453e800a8cf6dd2fc3eb4bc99b786a9b90c663b8559a5b1a041bf89e472" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "make-cmd" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8ca8afbe8af1785e09636acb5a41e08a765f5f0340568716c18a8700ba3c0d3" + +[[package]] +name = "pkg-config" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" diff --git a/contrib/libgit-sys/Cargo.toml b/contrib/libgit-sys/Cargo.toml new file mode 100644 index 0000000000..15b28c3585 --- /dev/null +++ b/contrib/libgit-sys/Cargo.toml @@ -0,0 +1,18 @@ +[package] +name = "libgit-sys" +version = "0.1.0" +edition = "2021" +build = "build.rs" +links = "gitpub" +rust-version = "1.63" +description = "Native bindings to a portion of libgit" + +[lib] +path = "src/lib.rs" + +[dependencies] +libz-sys = "1.1.19" + +[build-dependencies] +autocfg = "1.4.0" +make-cmd = "0.1.0" diff --git a/contrib/libgit-sys/README.md b/contrib/libgit-sys/README.md new file mode 100644 index 0000000000..c061cfcaf5 --- /dev/null +++ b/contrib/libgit-sys/README.md @@ -0,0 +1,4 @@ +# libgit-sys + +A small proof-of-concept crate showing how to provide a Rust FFI to Git +internals. diff --git a/contrib/libgit-sys/build.rs b/contrib/libgit-sys/build.rs new file mode 100644 index 0000000000..b6c65193bc --- /dev/null +++ b/contrib/libgit-sys/build.rs @@ -0,0 +1,35 @@ +use std::env; +use std::path::PathBuf; + +pub fn main() -> std::io::Result<()> { + let ac = autocfg::new(); + ac.emit_has_path("std::ffi::c_char"); + + let crate_root = PathBuf::from(env::var_os("CARGO_MANIFEST_DIR").unwrap()); + let git_root = crate_root.join("../.."); + let dst = PathBuf::from(env::var_os("OUT_DIR").unwrap()); + + let make_output = make_cmd::gnu_make() + .env("DEVELOPER", "1") + .env_remove("PROFILE") + .current_dir(git_root.clone()) + .args([ + "CFLAGS=-fvisibility=hidden", + "contrib/libgit-sys/libgitpub.a", + ]) + .output() + .expect("Make failed to run"); + if !make_output.status.success() { + panic!( + "Make failed:\n stdout = {}\n stderr = {}\n", + String::from_utf8(make_output.stdout).unwrap(), + String::from_utf8(make_output.stderr).unwrap() + ); + } + std::fs::copy(crate_root.join("libgitpub.a"), dst.join("libgitpub.a"))?; + println!("cargo:rustc-link-search=native={}", dst.display()); + println!("cargo:rustc-link-lib=gitpub"); + println!("cargo:rerun-if-changed={}", git_root.display()); + + Ok(()) +} diff --git a/contrib/libgit-sys/public_symbol_export.c b/contrib/libgit-sys/public_symbol_export.c new file mode 100644 index 0000000000..7cd5007902 --- /dev/null +++ b/contrib/libgit-sys/public_symbol_export.c @@ -0,0 +1,21 @@ +// Shim to publicly export Git symbols. These must be renamed so that the +// original symbols can be hidden. Renaming these with a "libgit_" prefix also +// avoids conflicts with other libraries such as libgit2. + +#include "git-compat-util.h" +#include "contrib/libgit-sys/public_symbol_export.h" +#include "version.h" + +#pragma GCC visibility push(default) + +const char *libgit_user_agent(void) +{ + return git_user_agent(); +} + +const char *libgit_user_agent_sanitized(void) +{ + return git_user_agent_sanitized(); +} + +#pragma GCC visibility pop diff --git a/contrib/libgit-sys/public_symbol_export.h b/contrib/libgit-sys/public_symbol_export.h new file mode 100644 index 0000000000..a3372f93fa --- /dev/null +++ b/contrib/libgit-sys/public_symbol_export.h @@ -0,0 +1,8 @@ +#ifndef PUBLIC_SYMBOL_EXPORT_H +#define PUBLIC_SYMBOL_EXPORT_H + +const char *libgit_user_agent(void); + +const char *libgit_user_agent_sanitized(void); + +#endif /* PUBLIC_SYMBOL_EXPORT_H */ diff --git a/contrib/libgit-sys/src/lib.rs b/contrib/libgit-sys/src/lib.rs new file mode 100644 index 0000000000..d4853f3074 --- /dev/null +++ b/contrib/libgit-sys/src/lib.rs @@ -0,0 +1,46 @@ +#[cfg(has_std__ffi__c_char)] +use std::ffi::c_char; + +#[cfg(not(has_std__ffi__c_char))] +#[allow(non_camel_case_types)] +pub type c_char = i8; + +extern crate libz_sys; + +extern "C" { + pub fn libgit_user_agent() -> *const c_char; + pub fn libgit_user_agent_sanitized() -> *const c_char; +} + +#[cfg(test)] +mod tests { + use std::ffi::CStr; + + use super::*; + + #[test] + fn user_agent_starts_with_git() { + let c_str = unsafe { CStr::from_ptr(libgit_user_agent()) }; + let agent = c_str + .to_str() + .expect("User agent contains invalid UTF-8 data"); + assert!( + agent.starts_with("git/"), + r#"Expected user agent to start with "git/", got: {}"#, + agent + ); + } + + #[test] + fn sanitized_user_agent_starts_with_git() { + let c_str = unsafe { CStr::from_ptr(libgit_user_agent_sanitized()) }; + let agent = c_str + .to_str() + .expect("Sanitized user agent contains invalid UTF-8 data"); + assert!( + agent.starts_with("git/"), + r#"Expected user agent to start with "git/", got: {}"#, + agent + ); + } +} From patchwork Wed Jan 15 20:05:42 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Steadmon X-Patchwork-Id: 13940873 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AD29A1D90AC for ; Wed, 15 Jan 2025 20:05:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736971555; cv=none; b=RV817KtSYtlCwuugdi+MQWHvBzfo9vMoWb2jlr07q9IjhVSR0v7N5CELAqiCR/q+nNQM+B486rPCJWm9hjxLyA5OR95Ts2oElojZpwe23jq5nOReMGkCMcPRUxNOECOOXUuQ3rNKKCfzD5vexmCDNpvOqjx5cLCir7TIZ5QDIWU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736971555; c=relaxed/simple; bh=QUara2RCrlV5RBdycoZ9Rc4EIVzU4MmDCGXWLXRjl7w=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Layqrjsvu9Gbjx2+L2AATJ0rfBC4ioLsO7aZgou+at6e6hzQ5s5vjACxUpBDOx+J0wQ20ZkiY3RqaK+4kPDq1XkHhXY1v86GqOHHfbtiE+hdQ/O6oeiKOqTwkEfjfnTYvswVRqkrrv4mXudmQsOcl6u+e6Fl0Zjdng1wVS5/qe0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--steadmon.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=QjgaAYTN; arc=none smtp.client-ip=209.85.214.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--steadmon.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="QjgaAYTN" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-216387ddda8so1242805ad.3 for ; Wed, 15 Jan 2025 12:05:53 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736971553; x=1737576353; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Fpt9H0neNvl8rmSiM1Q8p/j2QcPblK/XxzKQSDJ9OHk=; b=QjgaAYTNiIDe0/KJsDxQ3SqaMoFXIcXiqfwqAr+CASi+X8pl9gm1f946Q+1D+HvzML 7fxDbUyUF0YeWER+BsSj7dUR1fT3e8t+oynEcfaqH2njPOY/sFmKm6f07OC/yM8gw2xE tKPMm/hmBW2m+RqMVtqE5A8AG5qMkKZobA0cFj4N7pXY7RNpUcAvXYHDXgBDsm9LmYOy j8V8E3vHDI4ntOHG2EOJCTAO76C2J8dO8OCY8o9AxLR4secOE8C6InocmibmmtG62npg c0A8aWMsu8GD/MkUyVkHezgDQ3ZClcfBiO+Ezm9CclBntNZgDZkIz5AEwg1Exata6pIb uj3g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736971553; x=1737576353; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Fpt9H0neNvl8rmSiM1Q8p/j2QcPblK/XxzKQSDJ9OHk=; b=UEa30XFKClYJs0/sXr1hc8kc9KI5nZHLcvEl150HUl2ONRFDKPxlL6eOnrnBu4b6zx WdO/SlhCzSJUYjUZ9KiRg9j2YwMrZGnNRJ5NVI1sciMOU1MY3iQQxNIZLlSKcSuQoiPk scZz3Xn+4N+tAZ4Ch15oelvQ6sMriyAXBTpdPr4lG63oN7KDmnKAbJUG7Lt9VrKnwuc7 uP2wktDw4zx+IQK0mgt2tU9bjB3gItlEh+tDnzPveJvEp+ZuGSPQmkYz2YYqsOHZzdsD jjOY1zyxVZZ7sHHdEvFH58pgrW9UjMUCOat0REzcZyQ5B0Sr8iIPgTs8xhUK0l0xrKTy 6aLA== X-Gm-Message-State: AOJu0YwEPfeCpVq6zaIUbTBkvrWhuU6+jJB7Pw9paIJao0f8+q6m9avn WTrYltTmIft3MFXaKJDM8hqRJ4EHtxmgNH2xnLqZHyDlsFlPQ+AnQXJ5U/3ynemB7dafhDjGGsq g38zQv03TsNcW+rqzfyg4TMammEPkf4KFwmFxVSfy0NRE21U5t50CdKT7Cc/rozfyLFwXI41wap KRk3pN7UQPjhDXNFIW6LgmY3MW3gDGuXrm0lR18UU= X-Google-Smtp-Source: AGHT+IGEK2+k8R3AVS+xJMSIkEbVGZGOlLKGCty/Mr/AaHHeBa6wRvYP9faikYC4pU+Y9Orq1qIpJPcU6O98GQ== X-Received: from pgbbw32.prod.google.com ([2002:a05:6a02:4a0:b0:7fd:51fe:a923]) (user=steadmon job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:d81a:b0:1e3:e6f3:6372 with SMTP id adf61e73a8af0-1e88d132ef8mr48768743637.27.1736971552779; Wed, 15 Jan 2025 12:05:52 -0800 (PST) Date: Wed, 15 Jan 2025 12:05:42 -0800 In-Reply-To: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog Message-ID: <03f39b6c3ae8a36b3cedaa76d592af00491be1da.1736971328.git.steadmon@google.com> Subject: [PATCH v6 3/5] libgit-sys: also export some config_set functions From: Josh Steadmon To: git@vger.kernel.org Cc: calvinwan@google.com, nasamuffin@google.com, emrass@google.com, gitster@pobox.com, sandals@crustytoothpaste.net, mh@glandium.org, ps@pks.im, sunshine@sunshineco.com, phillip.wood123@gmail.com, allred.sean@gmail.com In preparation for implementing a higher-level Rust API for accessing Git configs, export some of the upstream configset API via libgitpub and libgit-sys. Since this will be exercised as part of the higher-level API in the next commit, no tests have been added for libgit-sys. While we're at it, add git_configset_alloc() and git_configset_free() functions in libgitpub so that callers can manage config_set structs on the heap. This also allows non-C external consumers to treat config_sets as opaque structs. Co-authored-by: Calvin Wan Signed-off-by: Calvin Wan Signed-off-by: Josh Steadmon --- contrib/libgit-sys/public_symbol_export.c | 29 +++++++++++++++++++++ contrib/libgit-sys/public_symbol_export.h | 10 ++++++++ contrib/libgit-sys/src/lib.rs | 31 ++++++++++++++++++++++- 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/contrib/libgit-sys/public_symbol_export.c b/contrib/libgit-sys/public_symbol_export.c index 7cd5007902..1799fc6fde 100644 --- a/contrib/libgit-sys/public_symbol_export.c +++ b/contrib/libgit-sys/public_symbol_export.c @@ -3,11 +3,40 @@ // avoids conflicts with other libraries such as libgit2. #include "git-compat-util.h" +#include "config.h" #include "contrib/libgit-sys/public_symbol_export.h" #include "version.h" #pragma GCC visibility push(default) +struct libgit_config_set *libgit_configset_alloc(void) +{ + struct config_set *cs = xmalloc(sizeof(struct config_set)); + git_configset_init(cs); + return (struct libgit_config_set *) cs; +} + +void libgit_configset_free(struct libgit_config_set *cs) +{ + git_configset_clear((struct config_set *) cs); + free((struct config_set *) cs); +} + +int libgit_configset_add_file(struct libgit_config_set *cs, const char *filename) +{ + return git_configset_add_file((struct config_set *) cs, filename); +} + +int libgit_configset_get_int(struct libgit_config_set *cs, const char *key, int *dest) +{ + return git_configset_get_int((struct config_set *) cs, key, dest); +} + +int libgit_configset_get_string(struct libgit_config_set *cs, const char *key, char **dest) +{ + return git_configset_get_string((struct config_set *) cs, key, dest); +} + const char *libgit_user_agent(void) { return git_user_agent(); diff --git a/contrib/libgit-sys/public_symbol_export.h b/contrib/libgit-sys/public_symbol_export.h index a3372f93fa..701db92d53 100644 --- a/contrib/libgit-sys/public_symbol_export.h +++ b/contrib/libgit-sys/public_symbol_export.h @@ -1,6 +1,16 @@ #ifndef PUBLIC_SYMBOL_EXPORT_H #define PUBLIC_SYMBOL_EXPORT_H +struct libgit_config_set *libgit_configset_alloc(void); + +void libgit_configset_free(struct libgit_config_set *cs); + +int libgit_configset_add_file(struct libgit_config_set *cs, const char *filename); + +int libgit_configset_get_int(struct libgit_config_set *cs, const char *key, int *dest); + +int libgit_configset_get_string(struct libgit_config_set *cs, const char *key, char **dest); + const char *libgit_user_agent(void); const char *libgit_user_agent_sanitized(void); diff --git a/contrib/libgit-sys/src/lib.rs b/contrib/libgit-sys/src/lib.rs index d4853f3074..dadb4e5f40 100644 --- a/contrib/libgit-sys/src/lib.rs +++ b/contrib/libgit-sys/src/lib.rs @@ -1,15 +1,44 @@ #[cfg(has_std__ffi__c_char)] -use std::ffi::c_char; +use std::ffi::{c_char, c_int}; #[cfg(not(has_std__ffi__c_char))] #[allow(non_camel_case_types)] pub type c_char = i8; +#[cfg(not(has_std__ffi__c_char))] +#[allow(non_camel_case_types)] +pub type c_int = i32; + extern crate libz_sys; +#[allow(non_camel_case_types)] +#[repr(C)] +pub struct libgit_config_set { + _data: [u8; 0], + _marker: core::marker::PhantomData<(*mut u8, core::marker::PhantomPinned)>, +} + extern "C" { pub fn libgit_user_agent() -> *const c_char; pub fn libgit_user_agent_sanitized() -> *const c_char; + + pub fn libgit_configset_alloc() -> *mut libgit_config_set; + pub fn libgit_configset_free(cs: *mut libgit_config_set); + + pub fn libgit_configset_add_file(cs: *mut libgit_config_set, filename: *const c_char) -> c_int; + + pub fn libgit_configset_get_int( + cs: *mut libgit_config_set, + key: *const c_char, + int: *mut c_int, + ) -> c_int; + + pub fn libgit_configset_get_string( + cs: *mut libgit_config_set, + key: *const c_char, + dest: *mut *mut c_char, + ) -> c_int; + } #[cfg(test)] From patchwork Wed Jan 15 20:05:43 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Steadmon X-Patchwork-Id: 13940874 Received: from mail-pj1-f73.google.com (mail-pj1-f73.google.com [209.85.216.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C077D1D90CB for ; Wed, 15 Jan 2025 20:05:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.216.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736971557; cv=none; b=XC5TiFxjDnRh26YMYn2UmBcLboCVHS2zBaeN004V7wd866gFPVsWdNctYp8lLadmEXkbLTfw0k8JIEoLbB2/7JOtTpKYa1k81uUu2DKTeJs2rVGN98OrVC2tvTI99+PlYHHzOiZV58SQwD/R6BUVzVFknhHs4KmNn1jt2eyvJHQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736971557; c=relaxed/simple; bh=LkjLM1nhHFTIApOga4w99yTF7A/SkxaG9yva1Kjwshc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=R5k1Ojb7vB5wVxfx6idorZ54VDIMjGSTgDCRjGPVJvZim5CQg5hsC6aPp0ZKt8tHtLf/475T1KA+fLY23UxJI5xeUOSG/fe2J6FTKYwXJwuyObyhxEmg5mzGy2r5cLYpvLZ/ljc5GdKnT911ubDjicA7LEB4RmZUVt6ZvJSQDmE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--steadmon.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ABNIY6S3; arc=none smtp.client-ip=209.85.216.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--steadmon.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ABNIY6S3" Received: by mail-pj1-f73.google.com with SMTP id 98e67ed59e1d1-2ef80d30df1so382571a91.1 for ; Wed, 15 Jan 2025 12:05:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736971555; x=1737576355; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=gE6L9R62t9MR0B3GyekEy3HsUzsDN79NYogjhUajJew=; b=ABNIY6S3539/mVbExW9aG1fjbqFJrkE9gJnXIpnTOSz/5LJkwqp3rSzS0oLRqfon2A ht6vqV1sXWjb9qZohJe1Q3X1y0Cl+cnK/06wOTmf+a2fJPEinpcWYzql/N9DZW5NGtsE aeekuVPdsikpuDLSXoYDzAPQpzWKzCyIwRKBnpuke2I5O7NRTk55xArsJcrWuupJja3H DZS63SWITWJSTWNf4sm8w5av4Ae3OEuLOIpwTPIKi81/UnxrWMJsOfh4D2DH5exhcrkG XVZiLU6Xzqf5clU8Qh1bIv5cHstd7w8ozB5v2wBRdrdD6U9foH6uGQVmrZdUVbSX5wzN DbnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736971555; x=1737576355; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=gE6L9R62t9MR0B3GyekEy3HsUzsDN79NYogjhUajJew=; b=WBwuyeFW15oY4qYJhl0CVOzVxd8R2CBr442z85bcrHim0Lwr+nrJfrr3LkP9VkA/7T t1cj8TJhOZ1DhqDVieSAkA2YY4kUSn9h3M9Fn3D9oNF+bRhpF84/I2DnuvRyJwaFY6Es AL67dD+kZ6EVmmlX0VVY2hoa3mhVHO56FZY8gcjLXLXtMXSOuXogkZMAgGWjHroFR+20 SY4u3t0lYZvCqlLmBKdIwPd0CS2s0R42U3582YgKUAHWHB4Zl55PcNib7+4OkmizoHnW VrP2X3PdWXj7V3rKmU2e0avbno8L+qwmdEd+x6HubJeEhpvJf0/2SODX7YpGwC4/4/Q/ zsVw== X-Gm-Message-State: AOJu0Yy/LGGx7P7csNhx4VIG9LtM4McijQHCA3Aaz/H678NfeX2K0bQQ l0LnH+rIAcNZQFnPeH3x+7aLxjY6WEAO2ZeZljKbjgWWwb8rE945iOSwhDZoPSdS7rX39xebVJY bZDc6Zsfs5SUL8ftfegng9mUd116jU10TqmfHAEj1HG9UrUr+Wt2mJYcx3Bv5lQoVJNHrLOSClI r/DhDJAlCphksfKosJNhO4NfnQPcfGNq592T+fj9I= X-Google-Smtp-Source: AGHT+IGKLr1d3yc1oi9nzXcVxuX7eoRGJUYOdOtc3fLb0SlR4nqFlaIjzDVpAslw0l5VIpniW1T3C2kc37BrkQ== X-Received: from pjz13.prod.google.com ([2002:a17:90b:56cd:b0:2f4:3eb4:f8bf]) (user=steadmon job=prod-delivery.src-stubby-dispatcher) by 2002:a17:90b:270d:b0:2ef:31a9:95c6 with SMTP id 98e67ed59e1d1-2f548ebf526mr47083883a91.14.1736971554844; Wed, 15 Jan 2025 12:05:54 -0800 (PST) Date: Wed, 15 Jan 2025 12:05:43 -0800 In-Reply-To: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog Message-ID: <65166ea0c077665c350a1a7b00dc3175be889d55.1736971328.git.steadmon@google.com> Subject: [PATCH v6 4/5] libgit: add higher-level libgit crate From: Josh Steadmon To: git@vger.kernel.org Cc: calvinwan@google.com, nasamuffin@google.com, emrass@google.com, gitster@pobox.com, sandals@crustytoothpaste.net, mh@glandium.org, ps@pks.im, sunshine@sunshineco.com, phillip.wood123@gmail.com, allred.sean@gmail.com From: Calvin Wan The C functions exported by libgit-sys do not provide an idiomatic Rust interface. To make it easier to use these functions via Rust, add a higher-level "libgit" crate, that wraps the lower-level configset API with an interface that is more Rust-y. This combination of $X and $X-sys crates is a common pattern for FFI in Rust, as documented in "The Cargo Book" [1]. [1] https://doc.rust-lang.org/cargo/reference/build-scripts.html#-sys-packages Co-authored-by: Josh Steadmon Signed-off-by: Calvin Wan Signed-off-by: Josh Steadmon --- .gitignore | 1 + Makefile | 2 +- contrib/libgit-rs/Cargo.lock | 77 ++++++++++++++++++++++++ contrib/libgit-rs/Cargo.toml | 15 +++++ contrib/libgit-rs/README.md | 13 ++++ contrib/libgit-rs/build.rs | 4 ++ contrib/libgit-rs/src/lib.rs | 95 ++++++++++++++++++++++++++++++ contrib/libgit-rs/testdata/config1 | 2 + contrib/libgit-rs/testdata/config2 | 2 + contrib/libgit-rs/testdata/config3 | 2 + contrib/libgit-sys/src/lib.rs | 4 ++ 11 files changed, 216 insertions(+), 1 deletion(-) create mode 100644 contrib/libgit-rs/Cargo.lock create mode 100644 contrib/libgit-rs/Cargo.toml create mode 100644 contrib/libgit-rs/README.md create mode 100644 contrib/libgit-rs/build.rs create mode 100644 contrib/libgit-rs/src/lib.rs create mode 100644 contrib/libgit-rs/testdata/config1 create mode 100644 contrib/libgit-rs/testdata/config2 create mode 100644 contrib/libgit-rs/testdata/config3 diff --git a/.gitignore b/.gitignore index 31d7e64287..acdd8ce7c7 100644 --- a/.gitignore +++ b/.gitignore @@ -250,4 +250,5 @@ Release/ /git.VC.db *.dSYM /contrib/buildsystems/out +/contrib/libgit-rs/target /contrib/libgit-sys/target diff --git a/Makefile b/Makefile index 47e864a861..230d366457 100644 --- a/Makefile +++ b/Makefile @@ -3729,7 +3729,7 @@ clean: profile-clean coverage-clean cocciclean $(RM) $(htmldocs).tar.gz $(manpages).tar.gz $(MAKE) -C Documentation/ clean $(RM) Documentation/GIT-EXCLUDED-PROGRAMS - $(RM) -r contrib/libgit-sys/target + $(RM) -r contrib/libgit-rs/target contrib/libgit-sys/target $(RM) -r contrib/libgit-sys/partial_symbol_export.o $(RM) -r contrib/libgit-sys/hidden_symbol_export.o $(RM) -r contrib/libgit-sys/libgitpub.a diff --git a/contrib/libgit-rs/Cargo.lock b/contrib/libgit-rs/Cargo.lock new file mode 100644 index 0000000000..a30c7c8d33 --- /dev/null +++ b/contrib/libgit-rs/Cargo.lock @@ -0,0 +1,77 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "autocfg" +version = "1.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26" + +[[package]] +name = "cc" +version = "1.1.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57b6a275aa2903740dc87da01c62040406b8812552e97129a63ea8850a17c6e6" +dependencies = [ + "shlex", +] + +[[package]] +name = "libc" +version = "0.2.158" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439" + +[[package]] +name = "libgit" +version = "0.1.0" +dependencies = [ + "autocfg", + "libgit-sys", +] + +[[package]] +name = "libgit-sys" +version = "0.1.0" +dependencies = [ + "autocfg", + "libz-sys", + "make-cmd", +] + +[[package]] +name = "libz-sys" +version = "1.1.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d2d16453e800a8cf6dd2fc3eb4bc99b786a9b90c663b8559a5b1a041bf89e472" +dependencies = [ + "cc", + "libc", + "pkg-config", + "vcpkg", +] + +[[package]] +name = "make-cmd" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a8ca8afbe8af1785e09636acb5a41e08a765f5f0340568716c18a8700ba3c0d3" + +[[package]] +name = "pkg-config" +version = "0.3.30" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec" + +[[package]] +name = "shlex" +version = "1.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" + +[[package]] +name = "vcpkg" +version = "0.2.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426" diff --git a/contrib/libgit-rs/Cargo.toml b/contrib/libgit-rs/Cargo.toml new file mode 100644 index 0000000000..634435cd6c --- /dev/null +++ b/contrib/libgit-rs/Cargo.toml @@ -0,0 +1,15 @@ +[package] +name = "libgit" +version = "0.1.0" +edition = "2021" +build = "build.rs" +rust-version = "1.63" + +[lib] +path = "src/lib.rs" + +[dependencies] +libgit-sys = { version = "0.1.0", path = "../libgit-sys" } + +[build-dependencies] +autocfg = "1.4.0" diff --git a/contrib/libgit-rs/README.md b/contrib/libgit-rs/README.md new file mode 100644 index 0000000000..ff945e1ce2 --- /dev/null +++ b/contrib/libgit-rs/README.md @@ -0,0 +1,13 @@ +# libgit-rs + +Proof-of-concept Git bindings for Rust. + +```toml +[dependencies] +libgit = "0.1.0" +``` + +## Rust version requirements + +libgit-rs should support Rust versions at least as old as the version included +in Debian stable (currently 1.63). diff --git a/contrib/libgit-rs/build.rs b/contrib/libgit-rs/build.rs new file mode 100644 index 0000000000..f8bd01a690 --- /dev/null +++ b/contrib/libgit-rs/build.rs @@ -0,0 +1,4 @@ +pub fn main() { + let ac = autocfg::new(); + ac.emit_has_path("std::ffi::c_char"); +} diff --git a/contrib/libgit-rs/src/lib.rs b/contrib/libgit-rs/src/lib.rs new file mode 100644 index 0000000000..27b6fd63f1 --- /dev/null +++ b/contrib/libgit-rs/src/lib.rs @@ -0,0 +1,95 @@ +use std::ffi::{c_void, CStr, CString}; +use std::path::Path; + +#[cfg(has_std__ffi__c_char)] +use std::ffi::{c_char, c_int}; + +#[cfg(not(has_std__ffi__c_char))] +#[allow(non_camel_case_types)] +pub type c_char = i8; + +#[cfg(not(has_std__ffi__c_char))] +#[allow(non_camel_case_types)] +pub type c_int = i32; + +use libgit_sys::*; + +pub struct ConfigSet(*mut libgit_config_set); +impl ConfigSet { + pub fn new() -> Self { + unsafe { ConfigSet(libgit_configset_alloc()) } + } + + pub fn add_files(&mut self, files: &[&Path]) { + for file in files { + let pstr = file.to_str().expect("Invalid UTF-8"); + let rs = CString::new(pstr).expect("Couldn't convert to CString"); + unsafe { + libgit_configset_add_file(self.0, rs.as_ptr()); + } + } + } + + pub fn get_int(&mut self, key: &str) -> Option { + let key = CString::new(key).expect("Couldn't convert to CString"); + let mut val: c_int = 0; + unsafe { + if libgit_configset_get_int(self.0, key.as_ptr(), &mut val as *mut c_int) != 0 { + return None; + } + } + + Some(val.into()) + } + + pub fn get_string(&mut self, key: &str) -> Option { + let key = CString::new(key).expect("Couldn't convert key to CString"); + let mut val: *mut c_char = std::ptr::null_mut(); + unsafe { + if libgit_configset_get_string(self.0, key.as_ptr(), &mut val as *mut *mut c_char) != 0 + { + return None; + } + let borrowed_str = CStr::from_ptr(val); + let owned_str = + String::from(borrowed_str.to_str().expect("Couldn't convert val to str")); + free(val as *mut c_void); // Free the xstrdup()ed pointer from the C side + Some(owned_str) + } + } +} + +impl Default for ConfigSet { + fn default() -> Self { + Self::new() + } +} + +impl Drop for ConfigSet { + fn drop(&mut self) { + unsafe { + libgit_configset_free(self.0); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn load_configs_via_configset() { + let mut cs = ConfigSet::new(); + cs.add_files(&[ + Path::new("testdata/config1"), + Path::new("testdata/config2"), + Path::new("testdata/config3"), + ]); + // ConfigSet retrieves correct value + assert_eq!(cs.get_int("trace2.eventTarget"), Some(1)); + // ConfigSet respects last config value set + assert_eq!(cs.get_int("trace2.eventNesting"), Some(3)); + // ConfigSet returns None for missing key + assert_eq!(cs.get_string("foo.bar"), None); + } +} diff --git a/contrib/libgit-rs/testdata/config1 b/contrib/libgit-rs/testdata/config1 new file mode 100644 index 0000000000..4e9a9d25d1 --- /dev/null +++ b/contrib/libgit-rs/testdata/config1 @@ -0,0 +1,2 @@ +[trace2] + eventNesting = 1 diff --git a/contrib/libgit-rs/testdata/config2 b/contrib/libgit-rs/testdata/config2 new file mode 100644 index 0000000000..b8d1eca423 --- /dev/null +++ b/contrib/libgit-rs/testdata/config2 @@ -0,0 +1,2 @@ +[trace2] + eventTarget = 1 diff --git a/contrib/libgit-rs/testdata/config3 b/contrib/libgit-rs/testdata/config3 new file mode 100644 index 0000000000..ca7b9a7c38 --- /dev/null +++ b/contrib/libgit-rs/testdata/config3 @@ -0,0 +1,2 @@ +[trace2] + eventNesting = 3 diff --git a/contrib/libgit-sys/src/lib.rs b/contrib/libgit-sys/src/lib.rs index dadb4e5f40..4bfc650450 100644 --- a/contrib/libgit-sys/src/lib.rs +++ b/contrib/libgit-sys/src/lib.rs @@ -1,3 +1,5 @@ +use std::ffi::c_void; + #[cfg(has_std__ffi__c_char)] use std::ffi::{c_char, c_int}; @@ -19,6 +21,8 @@ pub struct libgit_config_set { } extern "C" { + pub fn free(ptr: *mut c_void); + pub fn libgit_user_agent() -> *const c_char; pub fn libgit_user_agent_sanitized() -> *const c_char; From patchwork Wed Jan 15 20:05:44 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Steadmon X-Patchwork-Id: 13940875 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 936F61D89E4 for ; Wed, 15 Jan 2025 20:05:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736971559; cv=none; b=RnLb/YeUjMjffhzJADpxsKv6LSvys0gWsbT8U14Ai8oCTBBhYknjXb6LJAaPbkGeK7fzPDLkNv36+S6WmeFoHdhxcG6NBd+ZGr25T9KcBVH2l6PhotHWT+ydmcg1PrWNwoNeWuG55Pxa+HPVGz1ML91tgugvxNP660k8sF80fyU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736971559; c=relaxed/simple; bh=xkZfCi4X1Ldw/0SNtpoujoiCwgv8qj+6s5L+QDlMSDY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=LfYsK89bAXKw4o+i7MAU3TFJgBk7uAcplDO/RpSVjjTuk8t3c82phDWHgjvkrc7S2BMzQQBluJacvuc0+WAlFNXuIJmfKREH13l+3NRWpYPBSpkTCBESA0tU1Ba7aBcS4SD9sIzuwc9GD+abYqONNQO7sjEIfDooYqKFtSlgMMQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--steadmon.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=YbLXb/rz; arc=none smtp.client-ip=209.85.214.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--steadmon.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="YbLXb/rz" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2163a2a1ec2so2519385ad.1 for ; Wed, 15 Jan 2025 12:05:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736971557; x=1737576357; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=79fTpxMfs5oQJiK51bTPCuk7iBIsy1BLKvA0OCh8vfI=; b=YbLXb/rz37SB9kog/WaQPGIFdQdV7et9ugQZheum3yOpAoa5ElyekuAWXNKLu/Y6JB OHwaCZAE1+5s51LQGpaaXIBYI4SbBswbZkomrFFKCwfYULR7DB7fAtqCzaDvi7eL9Ck2 RiCHv9TK4Fa63OCcMIE9xxbhwTVBv2pfk5nscXiM8RIC8Co7IWRDBPNSmq+K4tXJF4Ah aYvCSMC0STDll4EgyHQxaDID2ECCzboFxpvq+DXGRXcFTN5h+NNSlHTV6w50zcSG+AZA jwUxc9JN406RvDaMYG2HVhb20VZVpZ5RppouTpHOm5T/S2eTQY8+lUrMRpQmk+iaNKA4 NvAw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736971557; x=1737576357; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=79fTpxMfs5oQJiK51bTPCuk7iBIsy1BLKvA0OCh8vfI=; b=QFJEcPU7yMkdB6ni0go39bCQEygUUA4636+vz1lDYKNMSVSmko70aviiZ8oYY2EUEv PXmR5hAfbsyPHWxfnbKBc2P1uwAHY3Xup6Oqldd0yUGOm2pFbeFsA8RAkn7RS3qeTR+6 +ZutdYKhH9qcdYyXJAiOZ4hUpHheAFNM76z7wKef3sW7oHt1ERSJhu7CvH/lCrg/vghJ nriNUj4AwSd82g0DwuZEDZTFHUxNZyMsQAbqOOdGUUyufDkoLk79WJxdYP1TbResQcdV LGOxxv37NoPopUdks3//HGbEymgpecDwKTj2RM3WLDyVihE3OjnH7BpliDp4PrU9Oo86 N4Lw== X-Gm-Message-State: AOJu0YwXZLbjtlHfl10gYNFgOvWYUp9G3ZWwcJUGelOVM+VP0B5jrNXx r9C7jCp+g/yiuvaTBv3arPrWVgeZm32c/jGxqrQLRlo78uSsSRd8+bIzhj1Ezmw9IfOyB63R0oi JuNHWLQ4OfJunzXgZEyCWX4amANmqOIAo2jnffTNrItKbSzaJcp+dNjtccyTFDZYSEaSHK2Gmgd QycSuglisfYCxdJ2tosyQnzd/7PeGlndy0klHpNGE= X-Google-Smtp-Source: AGHT+IF0EhhCqGp6jXXtxpMrIGqB0g/geeTv0PRd1dTHXIK23xBbtdsklv3RHBFrxU1gN9XbNuw0RzkefDkHmg== X-Received: from plbbb6.prod.google.com ([2002:a17:902:bc86:b0:215:5322:d126]) (user=steadmon job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:f681:b0:215:385e:921c with SMTP id d9443c01a7336-21a8400b31amr387117285ad.51.1736971556917; Wed, 15 Jan 2025 12:05:56 -0800 (PST) Date: Wed, 15 Jan 2025 12:05:44 -0800 In-Reply-To: Precedence: bulk X-Mailing-List: git@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog Message-ID: <84706f0db79fd83d93ea7b40e226300e69658d17.1736971328.git.steadmon@google.com> Subject: [PATCH v6 5/5] Makefile: add option to build and test libgit-rs and libgit-rs-sys From: Josh Steadmon To: git@vger.kernel.org Cc: calvinwan@google.com, nasamuffin@google.com, emrass@google.com, gitster@pobox.com, sandals@crustytoothpaste.net, mh@glandium.org, ps@pks.im, sunshine@sunshineco.com, phillip.wood123@gmail.com, allred.sean@gmail.com From: Calvin Wan Add libgitrs, libgitrs-sys, libgitrs-test, and libgitrs-sys-test targets to their respective Makefiles so they can be built and tested without having to run cargo build/test. Add build variable, INCLUDE_LIBGIT_RS, that when set, automatically builds and tests libgit-rs and libgit-rs-sys when `make all` is run. Co-authored-by: Josh Steadmon Signed-off-by: Calvin Wan Signed-off-by: Josh Steadmon --- Makefile | 26 ++++++++++++++++++++++++++ contrib/libgit-sys/build.rs | 2 +- t/Makefile | 16 ++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 230d366457..133090a145 100644 --- a/Makefile +++ b/Makefile @@ -416,6 +416,9 @@ include shared.mak # Define LINK_FUZZ_PROGRAMS if you want `make all` to also build the fuzz test # programs in oss-fuzz/. # +# Define INCLUDE_LIBGIT_RS if you want `make all` and `make test` to build and +# test the Rust crates in contrib/libgit-rs/ and contrib/libgit-rs/libgit-sys/. +# # === Optional library: libintl === # # Define NO_GETTEXT if you don't want Git output to be translated. @@ -2238,6 +2241,13 @@ ifdef FSMONITOR_OS_SETTINGS COMPAT_OBJS += compat/fsmonitor/fsm-path-utils-$(FSMONITOR_OS_SETTINGS).o endif +ifdef INCLUDE_LIBGIT_RS + # Enable symbol hiding in contrib/libgit-rs/libgit-sys/libgitpub.a + # without making us rebuild the whole tree every time we run a Rust + # build. + BASIC_CFLAGS += -fvisibility=hidden +endif + ifeq ($(TCLTK_PATH),) NO_TCLTK = NoThanks endif @@ -3895,6 +3905,22 @@ build-unit-tests: $(UNIT_TEST_PROGS) $(CLAR_TEST_PROG) unit-tests: $(UNIT_TEST_PROGS) $(CLAR_TEST_PROG) t/helper/test-tool$X $(MAKE) -C t/ unit-tests +.PHONY: libgit-sys +libgit-sys: + $(QUIET)(\ + cd contrib/libgit-sys && \ + cargo build \ + ) +.PHONY: libgit-rs +libgit-rs: + $(QUIET)(\ + cd contrib/libgit-rs && \ + cargo build \ + ) +ifdef INCLUDE_LIBGIT_RS +all:: libgit-rs +endif + contrib/libgit-sys/partial_symbol_export.o: contrib/libgit-sys/public_symbol_export.o libgit.a reftable/libreftable.a xdiff/lib.a $(LD) -r $^ -o $@ diff --git a/contrib/libgit-sys/build.rs b/contrib/libgit-sys/build.rs index b6c65193bc..3ffd80ad91 100644 --- a/contrib/libgit-sys/build.rs +++ b/contrib/libgit-sys/build.rs @@ -14,7 +14,7 @@ pub fn main() -> std::io::Result<()> { .env_remove("PROFILE") .current_dir(git_root.clone()) .args([ - "CFLAGS=-fvisibility=hidden", + "INCLUDE_LIBGIT_RS=YesPlease", "contrib/libgit-sys/libgitpub.a", ]) .output() diff --git a/t/Makefile b/t/Makefile index daa5fcae86..45fe6089b2 100644 --- a/t/Makefile +++ b/t/Makefile @@ -177,3 +177,19 @@ perf: .PHONY: pre-clean $(T) aggregate-results clean valgrind perf \ check-chainlint clean-chainlint test-chainlint $(UNIT_TESTS) + +.PHONY: libgit-sys-test +libgit-sys-test: + $(QUIET)(\ + cd ../contrib/libgit-sys && \ + cargo test \ + ) +.PHONY: libgit-rs-test +libgit-rs-test: + $(QUIET)(\ + cd ../contrib/libgit-rs && \ + cargo test \ + ) +ifdef INCLUDE_LIBGIT_RS +all:: libgit-sys-test libgit-rs-test +endif