From patchwork Tue Oct 15 22:50:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Steadmon X-Patchwork-Id: 13837351 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.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 BBE95146A9B for ; Tue, 15 Oct 2024 22:51:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729032664; cv=none; b=XmYzoPBg/o95xApBp/UiBWKZHg+7X9dCqez50qGyYBuEzXqRu4UXZfEqAE8kGN6VXMxFDCBN3Mp0lXjtGeUo+IjR1NlnKdkDft7ZpANecsRbIO0vx+bvQbh2vljgII6uRlJI3eZJoOuszolPZae1K6+7ReDV7kEJzZR6k8qb2ec= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729032664; c=relaxed/simple; bh=rJ38VFT2D+udVO01/6eDBewRt76MR3Z3WL3AskgaqrA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=YwpgAMvJxLdjPZjeDJYfSH/eL8AL0BqTClzfKwOHKltyPIsega22PJtqeqbNRQQIagW+/qVR/Hjdu85aXHEvdIMWUu6ndezwpTrMuAXWZppkyHzKVz69fakVo1lzHPtkgPB5UNUB/zmRB+VUAAJ8+PpZ/4u7ApvFe5zoFwAaBi0= 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=x9I7ko6r; arc=none smtp.client-ip=209.85.219.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="x9I7ko6r" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e2954ada861so3805923276.3 for ; Tue, 15 Oct 2024 15:51:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1729032661; x=1729637461; 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=L6P657FsvzoBA1eCjzW11XuMebINTa525UA19qDtlb4=; b=x9I7ko6r49MlFlOkyqfwkDgpXKE3yp/ZboPe6nmb35BqKwUPyYwoJkawbA07gOiTx3 6+HQwTyvKSI/MdFMCSbh0E5oUtdUcRz8pVgKtRg1oT09hCcEq93Uh/5nTgYR0gyYSoRz oKyJ+9ba2kj3sYrEiC7LXDB+eheR6DEZLVFm6LMbQhFice1GDxlPq8UZUXfyLZyeEu1Q JEtMBgfdWwFR3upCVHs/y59RjZLTpaTIaAqfaF2zQEf3Wh1R0Q6hHM32+UXhW4GbC8nb PsTjz9wh9BkouK69ZPlilV9dmd5INZ7a0BmRPmh+OZbUuvQeQatiynyFrvF4ogCBONfS Hi8w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729032661; x=1729637461; 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=L6P657FsvzoBA1eCjzW11XuMebINTa525UA19qDtlb4=; b=JautISZM59wDMmOAQfFCuVdQF/Y/NwXDJGB7qWvY5JjgJdghQCj9h510g4cNuwcIuJ 9DTrVSw6UmE4akRHGxnhoaqFd/lVuWrtJdSFU2hJt22iVa1TfHd6GaWdhyT2O7KTmN94 FSQuguMfTcpCUzCKjL7FN1nvCtCd054In7lBamlPeMT0tjAJ6wd7RysKeiiWQ+RRA5z1 q6tdAu/NpQt3nmrLTw9mOO/zX+mYp5gnfQBr9u73McBYqAnuCJbl1l4a0qvuo+wowB1b vbbmeypwQ8TJMUBgso4F2j58eM8uZ1Nq7mCWB18C0CqT1rUFHxeRhSFBEAoY4d8Y7TeN hVOQ== X-Gm-Message-State: AOJu0YyzNlBIlqwA6s2qFK32FscLf5xuu1bKNyidltcH8CmfhoW596Oa F4nqsgtdd9I7mYMPzyO+4Wapo/vpxkFuDWur7uMgNAkYrVeWCXhau+sZlNZ+XqfyxHtzM5tmaKx vN06awfxYm+UAD0F/1jSyPog/i6DVnjlhH026ueEHj6avCOXXFvYyhSSI9lu4mk6N1ltoy1vVqU GiqpuwaMYwgYmWLw/KfUs8FZLaTNZszvHSE1aBzNg= X-Google-Smtp-Source: AGHT+IEcR4CGp7IRk/K+R8WNn0oQrk9OYQce05a/Y3JOK1JU6uOTBlQjjFqlLWno28cDL1gbJK4kvVk5QKkn6A== X-Received: from lunarfall.svl.corp.google.com ([2620:15c:2d3:204:ddcc:6d3c:e4d6:3076]) (user=steadmon job=sendgmr) by 2002:a5b:f8d:0:b0:e28:fc1c:eb4d with SMTP id 3f1490d57ef6-e29782b2e7cmr2090276.1.1729032661278; Tue, 15 Oct 2024 15:51:01 -0700 (PDT) Date: Tue, 15 Oct 2024 15:50:53 -0700 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.47.0.rc1.288.g06298d1525-goog Message-ID: <1ae14207f69af3d1c6fe19dfd779062a69f16122.1729032373.git.steadmon@google.com> Subject: [PATCH v5 1/5] common-main: split init and exit code into new files From: Josh Steadmon To: git@vger.kernel.org Cc: calvinwan@google.com, emrass@google.com, gitster@pobox.com, mh@glandium.org, sandals@crustytoothpaste.net, ps@pks.im, sunshine@sunshineco.com, phillip.wood123@gmail.com, allred.sean@gmail.com, rsbecker@nexbridge.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 3eab701b10..7caeb3c872 100644 --- a/Makefile +++ b/Makefile @@ -979,6 +979,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 Tue Oct 15 22:50:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Steadmon X-Patchwork-Id: 13837352 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.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 4E80B1B6D07 for ; Tue, 15 Oct 2024 22:51:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729032666; cv=none; b=BIQ/9SBWtPmQA2ik9oL07AXyK9qUX1EY8Oa4OQ4TqHeOCgnFxrxUbDwpQ+1IJQcBxOPIo49p89RqORkDazcFQjORSwUL/qtHEh2jaloU0pzo3ecfYHKvz0uR4piTS4Npk/b1KYbHjrHAo37M6IwXITlOY89ew1/TVBnF00W0zvk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729032666; c=relaxed/simple; bh=QyLUGZVKHwBwxRa4BwFWnKoAh34+Tfnch8atxHP7FuE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=MojBPy8lyYDJusIBJfmgnPJfSYq6hXBJ9Tq9JTFNLdrNNq59TXYd74skrr0q19tH5tKl5z+ZmLLlqJsXPGBYCOgWbr7BcMdSkgL3p29ZLytsKhxrEjsofdx5p5punhRzj4vcU8c1C8ICiwP8RIG3iRt/UdgQEOdGcA1YJ5Iw6sE= 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=f6Xvhv2D; arc=none smtp.client-ip=209.85.219.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="f6Xvhv2D" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e291ac8dcf3so6626239276.3 for ; Tue, 15 Oct 2024 15:51:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1729032663; x=1729637463; 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=jhQqqYPIzzC2MYXnXMmrKAu3jWw8QvhgXPhNzt5Q4+c=; b=f6Xvhv2D34+LOgWk57EjschadpzOhdTmjZJ5r7iR6pp0O/T88wBcNzIoh/+tW2+tFw GAUeC+ag+27tJP2mpcxM0GvaaXafslmNobEyX5M41UHkdvAgz7670L831LXnvgrZZ7V4 q2Xo5u4rhM+MfDyJ4JFQNAQ2OMklyDyPwpV0rBtVHM2QVnfAVES9ay63RBfrcy+T1vjD CdjyN5PZef9NtGUCz/jqwVK5Duou2rCVr4awH3mLi50QXQYCEOtlw+HpqmHfemQJqV6e 49tjr27Sf3ZVZRsK5vOaKmqxywEKCR5A0O1ThpE9uGDSp/0W5wlxq4XNDUFEB3OCSYJG 8DIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729032663; x=1729637463; 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=jhQqqYPIzzC2MYXnXMmrKAu3jWw8QvhgXPhNzt5Q4+c=; b=AkbCNwyY4ugwymmBAu6pT1xbNHgv6TF+iQA4EmwKCryFP7g5e5vv8HFohyWHU/QZZR dgYgOcZq229gmbs5TJEIyJf58d5SH3z0+PR9GkigUY5djens2KOr/UDB0xXsxBYGwFVd Q/+K48RrTrF7b4i4p7gZzYCkLnn8IWzOK3DoXC7Bb61DrIgkalH7qNmclt/jDlYdrEPa pHCqJGQ7OMqTTOcw6avsXOT6dAPcxnZqJH8ogUU4HAkOdaf8tpY8B3eJImkrDtMy/mTP 9W0F8agEZG39t0J5XcxQdXtoo++fybsDCGX+foeXe0OTZmoG0whatX0t+yUBjazl0fyW FJOg== X-Gm-Message-State: AOJu0YzCESS8Cz92W12LX4mYc0yvovWtZo69tK11vcElVEApj3jBfWea i9dbxizkncH9tVKq+PzjTEpxb5Y+xmWhFfpAWaamJrh/ZKstdr27FD+D5ic6wXovrUPMrBG7zg+ xUJUc6s2gqTPkWy1Wy/+5tTv7/vsWnBCVzCKR6348HjBprD7cLyx2zdRJ9NH3bGoKD/NyQGNEKE QFVN+qapn5QzdZ1D6h7L82zFpU7eqFS9s8IOqnsCs= X-Google-Smtp-Source: AGHT+IFv/SUj/nIUPLPZ0s3flsJnx/eBdcNO4JlrTv7rOHQ6ByKUudjne7Kc1/Y2cBdC2cSrpEaMNSNf4U0tDQ== X-Received: from lunarfall.svl.corp.google.com ([2620:15c:2d3:204:ddcc:6d3c:e4d6:3076]) (user=steadmon job=sendgmr) by 2002:a25:6b43:0:b0:e1d:2043:da46 with SMTP id 3f1490d57ef6-e29782f3106mr1560276.3.1729032662888; Tue, 15 Oct 2024 15:51:02 -0700 (PDT) Date: Tue, 15 Oct 2024 15:50:54 -0700 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.47.0.rc1.288.g06298d1525-goog Message-ID: <1ed010c37852255e646b6dbb4beeac9c75c4cdde.1729032373.git.steadmon@google.com> Subject: [PATCH v5 2/5] libgit-sys: introduce Rust wrapper for libgit.a From: Josh Steadmon To: git@vger.kernel.org Cc: calvinwan@google.com, emrass@google.com, gitster@pobox.com, mh@glandium.org, sandals@crustytoothpaste.net, ps@pks.im, sunshine@sunshineco.com, phillip.wood123@gmail.com, allred.sean@gmail.com, rsbecker@nexbridge.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-rs/libgit-sys/Cargo.lock | 69 +++++++++++++++++++ contrib/libgit-rs/libgit-sys/Cargo.toml | 17 +++++ contrib/libgit-rs/libgit-sys/README.md | 15 ++++ contrib/libgit-rs/libgit-sys/build.rs | 35 ++++++++++ .../libgit-sys/public_symbol_export.c | 21 ++++++ .../libgit-sys/public_symbol_export.h | 8 +++ contrib/libgit-rs/libgit-sys/src/lib.rs | 46 +++++++++++++ 9 files changed, 228 insertions(+) create mode 100644 contrib/libgit-rs/libgit-sys/Cargo.lock create mode 100644 contrib/libgit-rs/libgit-sys/Cargo.toml create mode 100644 contrib/libgit-rs/libgit-sys/README.md create mode 100644 contrib/libgit-rs/libgit-sys/build.rs create mode 100644 contrib/libgit-rs/libgit-sys/public_symbol_export.c create mode 100644 contrib/libgit-rs/libgit-sys/public_symbol_export.h create mode 100644 contrib/libgit-rs/libgit-sys/src/lib.rs diff --git a/.gitignore b/.gitignore index 8caf3700c2..dfd72820fb 100644 --- a/.gitignore +++ b/.gitignore @@ -248,3 +248,4 @@ Release/ /git.VC.db *.dSYM /contrib/buildsystems/out +/contrib/libgit-rs/libgit-sys/target diff --git a/Makefile b/Makefile index 7caeb3c872..4f7467a8c2 100644 --- a/Makefile +++ b/Makefile @@ -653,6 +653,8 @@ CURL_CONFIG = curl-config GCOV = gcov STRIP = strip SPATCH = spatch +LD = ld +OBJCOPY = objcopy export TCL_PATH TCLTK_PATH @@ -2713,6 +2715,7 @@ OBJECTS += $(XDIFF_OBJS) OBJECTS += $(FUZZ_OBJS) OBJECTS += $(REFTABLE_OBJS) $(REFTABLE_TEST_OBJS) OBJECTS += $(UNIT_TEST_OBJS) +OBJECTS += contrib/libgit-rs/libgit-sys/public_symbol_export.o ifndef NO_CURL OBJECTS += http.o http-walker.o remote-curl.o @@ -3720,6 +3723,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-rs/libgit-sys/target + $(RM) -r contrib/libgit-rs/libgit-sys/partial_symbol_export.o + $(RM) -r contrib/libgit-rs/libgit-sys/hidden_symbol_export.o + $(RM) -r contrib/libgit-rs/libgit-sys/libgitpub.a ifndef NO_PERL $(RM) -r perl/build/ endif @@ -3865,3 +3872,12 @@ $(UNIT_TEST_PROGS): $(UNIT_TEST_BIN)/%$X: $(UNIT_TEST_DIR)/%.o \ build-unit-tests: $(UNIT_TEST_PROGS) unit-tests: $(UNIT_TEST_PROGS) t/helper/test-tool$X $(MAKE) -C t/ unit-tests + +contrib/libgit-rs/libgit-sys/partial_symbol_export.o: contrib/libgit-rs/libgit-sys/public_symbol_export.o libgit.a reftable/libreftable.a xdiff/lib.a + $(LD) -r $^ -o $@ + +contrib/libgit-rs/libgit-sys/hidden_symbol_export.o: contrib/libgit-rs/libgit-sys/partial_symbol_export.o + $(OBJCOPY) --localize-hidden $^ $@ + +contrib/libgit-rs/libgit-sys/libgitpub.a: contrib/libgit-rs/libgit-sys/hidden_symbol_export.o + $(AR) $(ARFLAGS) $@ $^ diff --git a/contrib/libgit-rs/libgit-sys/Cargo.lock b/contrib/libgit-rs/libgit-sys/Cargo.lock new file mode 100644 index 0000000000..427a4c66b7 --- /dev/null +++ b/contrib/libgit-rs/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-rs/libgit-sys/Cargo.toml b/contrib/libgit-rs/libgit-sys/Cargo.toml new file mode 100644 index 0000000000..7a230a4437 --- /dev/null +++ b/contrib/libgit-rs/libgit-sys/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "libgit-sys" +version = "0.1.0" +edition = "2021" +build = "build.rs" +links = "gitpub" +rust-version = "1.63" + +[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-rs/libgit-sys/README.md b/contrib/libgit-rs/libgit-sys/README.md new file mode 100644 index 0000000000..7a59602c30 --- /dev/null +++ b/contrib/libgit-rs/libgit-sys/README.md @@ -0,0 +1,15 @@ +# cgit-info + +A small hacky proof-of-concept showing how to provide a Rust FFI for the Git +library. + +## Building + +`cargo build` automatically builds and picks up on changes made to both +the Rust wrapper and git.git code so there is no need to run `make` +beforehand. + +## Running + +Assuming you don't make any changes to the Git source, you can just work from +`contrib/cgit-rs` and use `cargo build` or `cargo run` as usual. diff --git a/contrib/libgit-rs/libgit-sys/build.rs b/contrib/libgit-rs/libgit-sys/build.rs new file mode 100644 index 0000000000..8d74120191 --- /dev/null +++ b/contrib/libgit-rs/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-rs/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-rs/libgit-sys/public_symbol_export.c b/contrib/libgit-rs/libgit-sys/public_symbol_export.c new file mode 100644 index 0000000000..8e4b94d821 --- /dev/null +++ b/contrib/libgit-rs/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-rs/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-rs/libgit-sys/public_symbol_export.h b/contrib/libgit-rs/libgit-sys/public_symbol_export.h new file mode 100644 index 0000000000..a3372f93fa --- /dev/null +++ b/contrib/libgit-rs/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-rs/libgit-sys/src/lib.rs b/contrib/libgit-rs/libgit-sys/src/lib.rs new file mode 100644 index 0000000000..d4853f3074 --- /dev/null +++ b/contrib/libgit-rs/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 Tue Oct 15 22:50:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Steadmon X-Patchwork-Id: 13837353 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.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 99A0E1D63E0 for ; Tue, 15 Oct 2024 22:51:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729032668; cv=none; b=bdTxfxuzSn6wWqizLzK+Q702vQUdnVbLDjLQ74kdNKCpqtGIl6/Tf6mERJ+IoM9QHIbyyiY9o8fAEDF7sT/jc3FoiU77Kv7z1X54ukC2lkfGKeFWEIRdNZ+/oFFKrA51jRlZNn2Zfo6irNGL4TRU+pEZWSuzb6uJB1t4BvG0UPs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729032668; c=relaxed/simple; bh=ah+bGRBJF+407D7sNvycueUsnORlo3FFfRSnyRXISF4=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Po26aZ9lDJh9jNLA6U6eMpGo/4ZkaQBWyuJB3qbRox+qedK6T2CzMXlBftKLiUd6ZGqnFP/dzwQI4QMM8ytjxKgKuyJj74Waui5RtebFOz2MCT0UXr8hqQ0WEzi5UDectNYjko8aF8N7AfAnXYNJ9TVEhq8FSawSjiJYPrN938k= 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=IILMcqQf; arc=none smtp.client-ip=209.85.128.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="IILMcqQf" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-6e35bdb6a31so68334097b3.1 for ; Tue, 15 Oct 2024 15:51:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1729032665; x=1729637465; 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=mV63ka8xhQp3EHnR9vAoeu/+XItWOBDIaE0cjtAql+I=; b=IILMcqQfyJQorDfPZy4RaJJOR+RSz7DvinZ5vbLW/MKB+YgMCT+IMobMyjtwcJItaG OA25qUhr5BKO9XtIGCrXqV1sCMvE1OhTLaUhPOXAYMU8SWhmvRC8Q7MAoDx43Tzhf5vA NBO8B1Xi2ECk97klPTkhXgmgX1xtAgoMoeEgdfKQmqI7xrkDrBRKM7ABTaDH3uPHZubg d8BZsmEVxVBLBEJfQB+/b4EsRm0QsJSIR7wFPYDzOmC6e0HHoqekpdpBLCRaQWP0WRuF MODC6PkNJRllfFk8PPsBYO1x3tyqnd8beCNSboA+RRBUretCmBB/Iar0pwNaOHwAhcgP JSXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729032665; x=1729637465; 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=mV63ka8xhQp3EHnR9vAoeu/+XItWOBDIaE0cjtAql+I=; b=qajapRRa5HecWsoT/fbKi+aUZwkZ3yBgKb5IohPGVeHmqEkjNWntJmbpBgIjhb4Wpf p5il19eFk3MlItp5O8VGXQGtgYN7y3IFKjSF5KpUvK0ZMtZiQvpSWTlab3QRzH1YwT49 dY7N8uYVLoSrIeIynjmV9hseHMnqONlGnxbJwc89yInMKwZDVA90u7WD13bhKhh3pwN6 yga3uSEfr5w5VNV6wwTRJwBLZU6xfuzJEwUFMp99ne7OlZyrx9vFZKFNGLUbJiY16rCt otBMig+jQAqtI7YKEWt3F7RrczmF7+FVKxfoZqtD7FwEFOi4XfTIr7zO5gluwRdjUCLJ u03Q== X-Gm-Message-State: AOJu0YwJ2Rfyfvl/sbtxuVuon7/B3T92QUNoQkVwRQUW0v5BbzpaL/YJ qlZf01+mdDz26lnoK+UERYT9HLe+XWjZBJJFrdHGkQ1vuK0IrVuuLuX7kdu8uW7MsQrumwKyXcE 1pw3LZcTH8ZvD7ajR0tf9B0RhiJtHTMkZ6Rcrvz40P41U/OvwGTuh0n7IoDci8udY8c/0V5/Zjx UY/vDTEyVttovhzNQfruOHmZBkpzFMCHRMh6wY1jk= X-Google-Smtp-Source: AGHT+IFtiZ7PpuymWas9hdPODfz8i0+02Oxp1/RSMWkxiJz0tuRVtIxu8oahsSFN/33rht0HNFGW+QkisCuxCA== X-Received: from lunarfall.svl.corp.google.com ([2620:15c:2d3:204:ddcc:6d3c:e4d6:3076]) (user=steadmon job=sendgmr) by 2002:a25:b227:0:b0:e29:7473:3566 with SMTP id 3f1490d57ef6-e29786aa478mr864276.11.1729032664692; Tue, 15 Oct 2024 15:51:04 -0700 (PDT) Date: Tue, 15 Oct 2024 15:50:55 -0700 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.47.0.rc1.288.g06298d1525-goog Message-ID: <00762b57d032ddb80fb47ef277a164a46f1fc10f.1729032373.git.steadmon@google.com> Subject: [PATCH v5 3/5] libgit-sys: also export some config_set functions From: Josh Steadmon To: git@vger.kernel.org Cc: calvinwan@google.com, emrass@google.com, gitster@pobox.com, mh@glandium.org, sandals@crustytoothpaste.net, ps@pks.im, sunshine@sunshineco.com, phillip.wood123@gmail.com, allred.sean@gmail.com, rsbecker@nexbridge.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 --- .../libgit-sys/public_symbol_export.c | 29 +++++++++++++++++ .../libgit-sys/public_symbol_export.h | 10 ++++++ contrib/libgit-rs/libgit-sys/src/lib.rs | 31 ++++++++++++++++++- 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/contrib/libgit-rs/libgit-sys/public_symbol_export.c b/contrib/libgit-rs/libgit-sys/public_symbol_export.c index 8e4b94d821..29e8a5363e 100644 --- a/contrib/libgit-rs/libgit-sys/public_symbol_export.c +++ b/contrib/libgit-rs/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-rs/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-rs/libgit-sys/public_symbol_export.h b/contrib/libgit-rs/libgit-sys/public_symbol_export.h index a3372f93fa..701db92d53 100644 --- a/contrib/libgit-rs/libgit-sys/public_symbol_export.h +++ b/contrib/libgit-rs/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-rs/libgit-sys/src/lib.rs b/contrib/libgit-rs/libgit-sys/src/lib.rs index d4853f3074..dadb4e5f40 100644 --- a/contrib/libgit-rs/libgit-sys/src/lib.rs +++ b/contrib/libgit-rs/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 Tue Oct 15 22:50:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Steadmon X-Patchwork-Id: 13837354 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.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 5F3151B6D07 for ; Tue, 15 Oct 2024 22:51:08 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729032670; cv=none; b=G6VnOKXCIYjyJTK1Jlwbd6Khxm8903u0ExNhJPBQLHea1V6MIYf0QAcxvU4JdyD0QEF24BUCcoFOYXEvihkDCCHZtxJCqqelBZ8wyRk7DfiNBgaBxiOoby0sRDb1fKDeWpwTOYyhBZ7YvJJco0CGuW5f5prSNXSWm14zwnVEiDY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729032670; c=relaxed/simple; bh=BZ5a4diLVNOIgNUqMcdDRrA6zDTPuuIK7y7f5ROftIc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=nss/pvHzmWuOmt/sj1bXzjKHNruwNNfXYtgehFssc/DbBdNm/mxwnRJ3AqimgWyQDbcKJTWbsYn93oMsyU0OOJc1sIY0bqXBbV2tM2PtSk/5Odn92Hl8FkcCng3qjU+Iw6p8tVX3xW76h7qIKnYoMgCTxtonkSQMg6ilSgeMb2o= 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=tBNquLND; arc=none smtp.client-ip=209.85.219.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="tBNquLND" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e290222fde4so7344397276.1 for ; Tue, 15 Oct 2024 15:51:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1729032667; x=1729637467; 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=zl/HHd8cIYgdOg592T6ZChJFNliy6jly/+8Mks95eEU=; b=tBNquLND2AoWCD2FkMcr+fRuR2QpmRKXBmf+3c0k+tMFSVFvr3967VjBAJp3IQLzNW 5E/oeNwkULhEljIB1BZSYt9DPyKw4Ym6puVZwSCM6YFphANavE+GNTxnsyPGd/LETCd+ x7NqXuBjHLsNJGnLRJVJRIWIz2DCohBstndlQGfqFg0uEdsO/tdsIbfbJMJ9x0sp5kYD DkRcHrbPg6myK/M6aCC+T8Sb2P9nj7mB5/8cbGiN1yATb3Tr2yjPDxdpH2BrOanumCJv dLkv8Pn4K1iFkncpyRlpGalQpJpB69odMHe3jcmYAZODlGAFgyDxBbe1J74xxkNEmbIa Vrxg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729032667; x=1729637467; 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=zl/HHd8cIYgdOg592T6ZChJFNliy6jly/+8Mks95eEU=; b=RyWal5mY2xLuGLl1pyCysZP5RO6KYiijvdKPunqbPXQYXmST4wiOedJLhRQhiogiJi VRU+FIkeWeOe5ptXqNnwvagczPOmiX/wuRGNfFGldG5iV+/u54SaOt/Xgme+FEIKlf8b 5bFojmHSvXhWi9TTz++IjTvNPuObJQIgIUyyln/VUyEJgTkHUV0rwjDdsw6JCb2TXJHS Ah9jCjfzPIzCAOdlD+etqcWOAl9D/xDCjvoU5PqAzLCdHGTL0keZp2Rb6S6kSjVr2gMF oL0QNmnCrHLYskqhJlCdiOpD1nL1Mh0q6CGHdYTWrWjsRPuD3oh0rqg+gY/OUBJ0WkRI xP3g== X-Gm-Message-State: AOJu0Yw6UFmJTi5vS4DcmlF2mYkXAMVZHOEZljUWMPI69V3bEZPrn7H8 pVA5sfjLQZlRVS0ryfw8MG+y57gh+EEKDl8h2cqYYy2DZgkIdZZnK4A3txapaPQfNHiEvk8etP7 bJRCyMXW13QpYySc8VFFV239cxPRg27QXW0ZWhhgbsIZyS8vTRtC/gaHuVR75cyXfYhzkaMbJUb MNoyc/7SAdJshJHkFsn/Il9k5ri4/qK7C3vj2g3Fk= X-Google-Smtp-Source: AGHT+IE12snDSSl9s+3kMI6U32khk6S7uMnA0ERRFD1qFg0kK5s6G10ak9RWgXLBuYSJOWLtIrHhciu/MULyuQ== X-Received: from lunarfall.svl.corp.google.com ([2620:15c:2d3:204:ddcc:6d3c:e4d6:3076]) (user=steadmon job=sendgmr) by 2002:a25:b102:0:b0:e29:76f9:3d1d with SMTP id 3f1490d57ef6-e2978303dfbmr2201276.5.1729032666895; Tue, 15 Oct 2024 15:51:06 -0700 (PDT) Date: Tue, 15 Oct 2024 15:50:56 -0700 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.47.0.rc1.288.g06298d1525-goog Message-ID: <4e5360931b74a6c3ee60e3b648a9757d1f9c94f5.1729032373.git.steadmon@google.com> Subject: [PATCH v5 4/5] libgit: add higher-level libgit crate From: Josh Steadmon To: git@vger.kernel.org Cc: calvinwan@google.com, emrass@google.com, gitster@pobox.com, mh@glandium.org, sandals@crustytoothpaste.net, ps@pks.im, sunshine@sunshineco.com, phillip.wood123@gmail.com, allred.sean@gmail.com, rsbecker@nexbridge.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: Josh Steadmon Signed-off-by: Calvin Wan --- .gitignore | 1 + Makefile | 2 +- contrib/libgit-rs/Cargo.lock | 77 ++++++++++++++++++++ contrib/libgit-rs/Cargo.toml | 15 ++++ contrib/libgit-rs/build.rs | 4 ++ contrib/libgit-rs/libgit-sys/src/lib.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 + 10 files changed, 203 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/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 dfd72820fb..0a42f27117 100644 --- a/.gitignore +++ b/.gitignore @@ -248,4 +248,5 @@ Release/ /git.VC.db *.dSYM /contrib/buildsystems/out +/contrib/libgit-rs/target /contrib/libgit-rs/libgit-sys/target diff --git a/Makefile b/Makefile index 4f7467a8c2..1790c737bd 100644 --- a/Makefile +++ b/Makefile @@ -3723,7 +3723,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-rs/libgit-sys/target + $(RM) -r contrib/libgit-rs/target contrib/libgit-rs/libgit-sys/target $(RM) -r contrib/libgit-rs/libgit-sys/partial_symbol_export.o $(RM) -r contrib/libgit-rs/libgit-sys/hidden_symbol_export.o $(RM) -r contrib/libgit-rs/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..6b4387b9e7 --- /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/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/libgit-sys/src/lib.rs b/contrib/libgit-rs/libgit-sys/src/lib.rs index dadb4e5f40..4bfc650450 100644 --- a/contrib/libgit-rs/libgit-sys/src/lib.rs +++ b/contrib/libgit-rs/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; 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 From patchwork Tue Oct 15 22:50:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Josh Steadmon X-Patchwork-Id: 13837355 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.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 38B211B6CEE for ; Tue, 15 Oct 2024 22:51:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.202 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729032672; cv=none; b=rl/mRq0AEhkzcYWyigrY7UgjVydLJkuBfv1EEEGeIKR5WjkkrvlppCboeEmYvHehJMzlNa9lI56awArqqYviNBTCws34YwYq4/JY8TGjUvPW9de3GiuMWD/nVIMhIFykH3Lrn1ibnpLvl+9CNacAxJ5OZwyp2eSt3rSk1d+16PY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1729032672; c=relaxed/simple; bh=5oOdSD4Q1+6jV5rEDdFE+B0q0ZJyFB+3y4Fxjzp9r/8=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=PMY5CIPcLHR8TW5SG+wcdov/tjVqUf/WaF7ybOSvRM1/kzIeyfkb9NHsCe6rE/+9Hicn7rRpW+tv7TDMzBf0PjjruONhfd4k2/VU10N2TxKHzK8rVj7DNkZoeBEOKRXD17v2JDzyZkYFIDz/zbw6QVgAf0OAGts809vwTL+E3vs= 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=QIvRp/1A; arc=none smtp.client-ip=209.85.219.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="QIvRp/1A" Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-e1159159528so548714276.1 for ; Tue, 15 Oct 2024 15:51:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1729032669; x=1729637469; 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=8OCxwM4M+ooQN/ssD4sIZCmHX2H5Z5zktY4rJthYT9U=; b=QIvRp/1AoU21N7v2W3nylhp5xO4qQPl5vAGSemZXWJW/5haDvKMZgvCFR8wSP0rhnc 27i6MJODiLA96Qv3LNSFBAvbRAErECyulBKl64GI4kJ3A8Kvb/S0kv1k6XbDEnFtKDay sLUimcC9KBagvjjwji1fuTGEu5WErgMeK23INcPEFV7cPK4OsKaMmzEe5xqLGA8kS6dN fJIe4pJdxzJFmRkxT7JWLP43cxT/9a3S+QX7+3gSmRTe+RNPnSa2WZ2SF0bXq7JnlhgQ ZYBG0qylTcFM9YpjrfENooUhTJua1UgVO93fZ5Aa5/BSd2UfFVxaR9Hpm2otqgDgN18Z 6pTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729032669; x=1729637469; 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=8OCxwM4M+ooQN/ssD4sIZCmHX2H5Z5zktY4rJthYT9U=; b=DEhYpUkNPEQqRypx3v1Sb6RYIyam1tQolapiS25AkUr++Uimu2aLHCmXrP9t3lyTR+ Uv7SSJRThuDXAfZ3pDceYOSFme2+69LTR1wHK8v0nI7aLR7/CjQrpcelHfVNk8Hdt1S8 oJaXucNW0cyXMdU0FYxloxlKs2QfibFLW8J98zbOPqip5trKF8tkwtIiAaGIqra/aDlP e8q/P6b0z1IX1gp6Rh4USyblMYUvDjkRYiSTzH0fjkNY//RdPFblE8UFVBHwznfogO7i nB1tf50CwqS5r6Tx1pngIAT2dr8/39/NEPiCkGbyi4Z4ppQHCJ5aXb/S1P732tW4zKUu U1mA== X-Gm-Message-State: AOJu0YxONCp/bKORilUCJAVrNF75Yrr0dXFY5/1OPLFi/D2/RqLvbUWe GBVnyhBOOh5E4Aj2Wr8cTuFrULmPIaGG2SxqHreoDuky0Wyrqc4avhV5nL9Do9YnEcXIR8VT7rE bYuCbv6owiTlMyh8Ti6rfsDjt2S1w+QFphrKXNWUnHD+zsbwA54RHHLo6GBNCBFM2hx2SdMC61u l8HIyxJ8bR8VhNsIx6zqKKqwVY5VfFpxZRxhNgDMw= X-Google-Smtp-Source: AGHT+IGGspnFbl1YrP4f2VlglnZ2MhgIjkfGP6lq7TtuTf3vhoM6E4M95oftfysUi4/3k8jxFA5IfqPydZAang== X-Received: from lunarfall.svl.corp.google.com ([2620:15c:2d3:204:ddcc:6d3c:e4d6:3076]) (user=steadmon job=sendgmr) by 2002:a25:eb01:0:b0:e24:b971:c4cb with SMTP id 3f1490d57ef6-e29772b8abcmr7292276.2.1729032668827; Tue, 15 Oct 2024 15:51:08 -0700 (PDT) Date: Tue, 15 Oct 2024 15:50:57 -0700 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.47.0.rc1.288.g06298d1525-goog Message-ID: <15ce989de8bff1080a018fd9dee678856180a45f.1729032373.git.steadmon@google.com> Subject: [PATCH v5 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, emrass@google.com, gitster@pobox.com, mh@glandium.org, sandals@crustytoothpaste.net, ps@pks.im, sunshine@sunshineco.com, phillip.wood123@gmail.com, allred.sean@gmail.com, rsbecker@nexbridge.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-rs/libgit-sys/build.rs | 2 +- t/Makefile | 16 ++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 1790c737bd..f191ee32bb 100644 --- a/Makefile +++ b/Makefile @@ -412,6 +412,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. @@ -2181,6 +2184,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 @@ -3873,6 +3883,22 @@ build-unit-tests: $(UNIT_TEST_PROGS) unit-tests: $(UNIT_TEST_PROGS) t/helper/test-tool$X $(MAKE) -C t/ unit-tests +.PHONY: libgitrs-sys +libgitrs-sys: + $(QUIET)(\ + cd contrib/libgit-rs/libgit-sys && \ + cargo build \ + ) +.PHONY: libgitrs +libgitrs: + $(QUIET)(\ + cd contrib/libgit-rs && \ + cargo build \ + ) +ifdef INCLUDE_LIBGIT_RS +all:: libgitrs +endif + contrib/libgit-rs/libgit-sys/partial_symbol_export.o: contrib/libgit-rs/libgit-sys/public_symbol_export.o libgit.a reftable/libreftable.a xdiff/lib.a $(LD) -r $^ -o $@ diff --git a/contrib/libgit-rs/libgit-sys/build.rs b/contrib/libgit-rs/libgit-sys/build.rs index 8d74120191..d6d6210ab2 100644 --- a/contrib/libgit-rs/libgit-sys/build.rs +++ b/contrib/libgit-rs/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-rs/libgit-sys/libgitpub.a", ]) .output() diff --git a/t/Makefile b/t/Makefile index b2eb9f770b..066cb5c2b4 100644 --- a/t/Makefile +++ b/t/Makefile @@ -169,3 +169,19 @@ perf: .PHONY: pre-clean $(T) aggregate-results clean valgrind perf \ check-chainlint clean-chainlint test-chainlint $(UNIT_TESTS) + +.PHONY: libgitrs-sys-test +libgitrs-sys-test: + $(QUIET)(\ + cd ../contrib/libgit-rs/libgit-sys && \ + cargo test \ + ) +.PHONY: libgitrs-test +libgitrs-test: + $(QUIET)(\ + cd ../contrib/libgit-rs && \ + cargo test \ + ) +ifdef INCLUDE_LIBGIT_RS +all:: libgitrs-sys-test libgitrs-test +endif