Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Fcitx bazel build #68

Open
wants to merge 1 commit into
base: fcitx
Choose a base branch
from

Conversation

phoepsilonix
Copy link

Description

This is a fix to enable fcitx-mozc to be built with bazel.

@phoepsilonix phoepsilonix marked this pull request as ready for review October 26, 2024 02:16
phoepsilonix added a commit to TachiBanaHime/fcitx-mozc that referenced this pull request Oct 26, 2024
  * add build_fcitx_bazel

  * This is a fix to enable fcitx-mozc to be built with bazel.
phoepsilonix added a commit to TachiBanaHime/fcitx-mozc that referenced this pull request Oct 26, 2024
  * add build_fcitx_bazel

  * This is a fix to enable fcitx-mozc to be built with bazel.
@wengxt
Copy link
Member

wengxt commented Oct 26, 2024

you definitely break the build by remove @fcitx

@phoepsilonix
Copy link
Author

phoepsilonix commented Oct 26, 2024

you definitely break the build by remove @fcitx

I certainly thought so too, but in my environment, when @fcitx is present, bazel cannot find fcitx and an error occurs, and when I remove @fcitx, the build can be completed to the end. I am trying to build with fcitx and fcitx-qt6 installed. I thought that bazel's reaction was different due to the description of pkg-config or something, but is there something wrong with my environment? I am trying to build in multiple environments, such as podman, but...

In the github workflow, fcitx-mozc is built with bazel on arch, manjaro, ubuntu, and debian. If I remove @fcitx from BUILD.bazel, no errors occur and the build completes.

I suspect that the fcitx-related files shown by pkg-config are not recognized as @fcitx (:fcitx).

@wengxt
Copy link
Member

wengxt commented Oct 26, 2024

No, the pkg-config should just work.

I even used a non standard prefix to test.

@phoepsilonix
Copy link
Author

phoepsilonix commented Oct 26, 2024

Of course, pkg-config is working. Bazel should have the library information for fcitx-mozc to link against.
The difference with fcitx5 is that fcitx can usually be compiled with -I/usr/include. For fcitx, pkg-config doesn't need to show cflags unless you use --keep-system-cflags to include headers.

So I suspect it's being handled differently.

pkg-config --cflags --libs Fcitx5Core
-I/usr/include/Fcitx5/Core -I/usr/include/Fcitx5/Config -I/usr/include/Fcitx5/Utils -lFcitx5Core -lFcitx5Config -lFcitx5Utils
pkg-config --cflags --libs fcitx
-lfcitx-core -lfcitx-config -lfcitx-utils

@phoepsilonix
Copy link
Author

bazel cquery --config oss_linux --config release_build  "deps(//unix/fcitx:fcitx-mozc.so)" --output graph > specific_deps.dot
Computing main repo mapping: 
Loading: 
Loading: 0 packages loaded
Analyzing: target //unix/fcitx:fcitx-mozc.so (1 packages loaded, 0 targets configured)
ERROR: Traceback (most recent call last):
	File "/home/phoepsilonix/.cache/bazel/_bazel_phoepsilonix/a422ec5eb6e7740277eb4ee8963a4986/external/_main~_repo_rules~fcitx/BUILD.bazel", line 8, column 16, in <toplevel>
		hdrs = glob([
Error in glob: pattern cannot be absolute
ERROR: /home/phoepsilonix/.cache/bazel/_bazel_phoepsilonix/a422ec5eb6e7740277eb4ee8963a4986/external/_main~_repo_rules~fcitx/BUILD.bazel: no such target '@@_main~_repo_rules~fcitx//:fcitx': target 'fcitx' not declared in package '' defined by /home/phoepsilonix/.cache/bazel/_bazel_phoepsilonix/a422ec5eb6e7740277eb4ee8963a4986/external/_main~_repo_rules~fcitx/BUILD.bazel
ERROR: /home/phoepsilonix/work/aur/mozc-with-jp-dict/src/mozc/src/unix/fcitx/BUILD.bazel:89:15: no such target '@@_main~_repo_rules~fcitx//:fcitx': target 'fcitx' not declared in package '' defined by /home/phoepsilonix/.cache/bazel/_bazel_phoepsilonix/a422ec5eb6e7740277eb4ee8963a4986/external/_main~_repo_rules~fcitx/BUILD.bazel and referenced by '//unix/fcitx:fcitx-mozc.so'
ERROR: Analysis of target '//unix/fcitx:fcitx-mozc.so' failed; build aborted: Analysis failed
INFO: Elapsed time: 0.098s
INFO: 0 processes.
ERROR: Build did NOT complete successfully

when remove @fcitx

 bazel cquery --config oss_linux --config release_build  "deps(//unix/fcitx:fcitx-mozc.so)" --output graph > specific_deps.dot
Computing main repo mapping: 
Loading: 
Loading: 0 packages loaded
Analyzing: target //unix/fcitx:fcitx-mozc.so (1 packages loaded, 0 targets configured)
INFO: Analyzed target //unix/fcitx:fcitx-mozc.so (1 packages loaded, 18 targets configured).
INFO: Found 1 target...
INFO: Elapsed time: 0.318s, Critical Path: 0.00s
INFO: 0 processes.
INFO: Build completed successfully, 0 total actions
-  "//unix/fcitx:fcitx-mozc.so (914686d)"
  "//unix/fcitx:fcitx-mozc.so (914686d)" -> "//:compiler_msvc_like (914686d)"
  "//unix/fcitx:fcitx-mozc.so (914686d)" -> "//:macro (914686d)"
  "//unix/fcitx:fcitx-mozc.so (914686d)" -> "//base:init_mozc (914686d)"
  "//unix/fcitx:fcitx-mozc.so (914686d)" -> "//unix/fcitx:eim.cc (null)"
  "//unix/fcitx:fcitx-mozc.so (914686d)" -> "//unix/fcitx:fcitx_mozc (914686d)"
  "//unix/fcitx:fcitx-mozc.so (914686d)" -> "@bazel_tools//tools:host_platform (914686d)\n@bazel_tools//tools:host_platform (ab8cba1)"
  "//unix/fcitx:fcitx-mozc.so (914686d)" -> "@bazel_tools//tools/cpp:current_cc_toolchain (914686d)"
  "//unix/fcitx:fcitx-mozc.so (914686d)" -> "@bazel_tools//tools/cpp:link_extra_lib (914686d)"
  "//unix/fcitx:fcitx-mozc.so (914686d)" -> "@bazel_tools//tools/cpp:malloc (914686d)"
  "//unix/fcitx:fcitx-mozc.so (914686d)" -> "@bazel_tools//tools/cpp:toolchain_type (914686d)"
  "//unix/fcitx:fcitx-mozc.so (914686d)" -> "@bazel_tools//tools/def_parser:def_parser (f9c3c74)"
  "//unix/fcitx:fcitx-mozc.so (914686d)" -> "@@bazel_tools~cc_configure_extension~local_config_cc//:cc-compiler-k8 (914686d)"
  "//unix/fcitx:fcitx-mozc.so (914686d)" -> "@platforms//host:host (ab8cba1)"
  "//unix/fcitx:fcitx_mozc (914686d)"
  "//unix/fcitx:fcitx_mozc (914686d)" -> "//:compiler_msvc_like (914686d)"
  "//unix/fcitx:fcitx_mozc (914686d)" -> "//:macro (914686d)"
  "//unix/fcitx:fcitx_mozc (914686d)" -> "//base:port (914686d)"
  "//unix/fcitx:fcitx_mozc (914686d)" -> "//base:process (914686d)"
  "//unix/fcitx:fcitx_mozc (914686d)" -> "//base:run_level (914686d)"
  "//unix/fcitx:fcitx_mozc (914686d)" -> "//base:util (914686d)"
  "//unix/fcitx:fcitx_mozc (914686d)" -> "//base:vlog (914686d)"
  "//unix/fcitx:fcitx_mozc (914686d)" -> "//client:client_interface (914686d)"
  "//unix/fcitx:fcitx_mozc (914686d)" -> "//protocol:commands_cc_proto (914686d)"
  "//unix/fcitx:fcitx_mozc (914686d)" -> "//unix/fcitx:fcitx_key_util (914686d)"
  "//unix/fcitx:fcitx_mozc (914686d)" -> "//unix/fcitx:fcitx_mozc.cc (null)\n//unix/fcitx:fcitx_mozc.h (null)\n//unix/fcitx:mozc_response_parser.cc (null)\n//unix/fcitx:mozc_response_parser.h (null)"
  "//unix/fcitx:fcitx_mozc (914686d)" -> "//unix/fcitx:mozc_connection (914686d)"
  "//unix/fcitx:fcitx_mozc (914686d)" -> "//unix/fcitx:surrounding_text_util (914686d)"
  "//unix/fcitx:fcitx_mozc (914686d)" -> "@com_google_absl//absl/log:check (914686d)"
  "//unix/fcitx:fcitx_mozc (914686d)" -> "@bazel_tools//tools:host_platform (914686d)\n@bazel_tools//tools:host_platform (ab8cba1)"
  "//unix/fcitx:fcitx_mozc (914686d)" -> "@bazel_tools//tools/cpp:current_cc_toolchain (914686d)"
  "//unix/fcitx:fcitx_mozc (914686d)" -> "@bazel_tools//tools/cpp:toolchain_type (914686d)"
  "//unix/fcitx:fcitx_mozc (914686d)" -> "@bazel_tools//tools/def_parser:def_parser (f9c3c74)"
  "//unix/fcitx:fcitx_mozc (914686d)" -> "@@bazel_tools~cc_configure_extension~local_config_cc//:cc-compiler-k8 (914686d)"
  "//unix/fcitx:fcitx_mozc (914686d)" -> "@platforms//host:host (ab8cba1)"
  "//unix/fcitx:mozc_connection (914686d)"
  "//unix/fcitx:mozc_connection (914686d)" -> "//:compiler_msvc_like (914686d)"
  "//unix/fcitx:mozc_connection (914686d)" -> "//:macro (914686d)"
  "//unix/fcitx:mozc_connection (914686d)" -> "//base:port (914686d)"
  "//unix/fcitx:mozc_connection (914686d)" -> "//base:util (914686d)"
  "//unix/fcitx:mozc_connection (914686d)" -> "//base:vlog (914686d)"
  "//unix/fcitx:mozc_connection (914686d)" -> "//client:client (914686d)"
  "//unix/fcitx:mozc_connection (914686d)" -> "//protocol:commands_cc_proto (914686d)"
  "//unix/fcitx:mozc_connection (914686d)" -> "//unix/fcitx:fcitx_key_util (914686d)"
  "//unix/fcitx:mozc_connection (914686d)" -> "//unix/fcitx:mozc_connection.cc (null)\n//unix/fcitx:mozc_connection.h (null)"
  "//unix/fcitx:mozc_connection (914686d)" -> "//unix/fcitx:surrounding_text_util (914686d)"
  "//unix/fcitx:mozc_connection (914686d)" -> "@com_google_absl//absl/log:check (914686d)"
  "//unix/fcitx:mozc_connection (914686d)" -> "@bazel_tools//tools:host_platform (914686d)\n@bazel_tools//tools:host_platform (ab8cba1)"
  "//unix/fcitx:mozc_connection (914686d)" -> "@bazel_tools//tools/cpp:current_cc_toolchain (914686d)"
  "//unix/fcitx:mozc_connection (914686d)" -> "@bazel_tools//tools/cpp:toolchain_type (914686d)"
  "//unix/fcitx:mozc_connection (914686d)" -> "@bazel_tools//tools/def_parser:def_parser (f9c3c74)"
  "//unix/fcitx:mozc_connection (914686d)" -> "@@bazel_tools~cc_configure_extension~local_config_cc//:cc-compiler-k8 (914686d)"
  "//unix/fcitx:mozc_connection (914686d)" -> "@platforms//host:host (ab8cba1)"
  "//unix/fcitx:surrounding_text_util (914686d)"
  "//unix/fcitx:surrounding_text_util (914686d)" -> "//:compiler_msvc_like (914686d)"
  "//unix/fcitx:surrounding_text_util (914686d)" -> "//:macro (914686d)"
  "//unix/fcitx:surrounding_text_util (914686d)" -> "//base:port (914686d)"
  "//unix/fcitx:surrounding_text_util (914686d)" -> "//base:util (914686d)"
  "//unix/fcitx:surrounding_text_util (914686d)" -> "//base:vlog (914686d)"
  "//unix/fcitx:surrounding_text_util (914686d)" -> "//unix/fcitx:surrounding_text_util.cc (null)\n//unix/fcitx:surrounding_text_util.h (null)"
  "//unix/fcitx:surrounding_text_util (914686d)" -> "@com_google_absl//absl/log:check (914686d)"
  "//unix/fcitx:surrounding_text_util (914686d)" -> "@bazel_tools//tools:host_platform (914686d)\n@bazel_tools//tools:host_platform (ab8cba1)"
  "//unix/fcitx:surrounding_text_util (914686d)" -> "@bazel_tools//tools/cpp:current_cc_toolchain (914686d)"
  "//unix/fcitx:surrounding_text_util (914686d)" -> "@bazel_tools//tools/cpp:toolchain_type (914686d)"
  "//unix/fcitx:surrounding_text_util (914686d)" -> "@bazel_tools//tools/def_parser:def_parser (f9c3c74)"
  "//unix/fcitx:surrounding_text_util (914686d)" -> "@@bazel_tools~cc_configure_extension~local_config_cc//:cc-compiler-k8 (914686d)"
  "//unix/fcitx:surrounding_text_util (914686d)" -> "@platforms//host:host (ab8cba1)"
  "//unix/fcitx:surrounding_text_util.cc (null)\n//unix/fcitx:surrounding_text_util.h (null)"
  "//unix/fcitx:mozc_connection.cc (null)\n//unix/fcitx:mozc_connection.h (null)"
  "//unix/fcitx:fcitx_mozc.cc (null)\n//unix/fcitx:fcitx_mozc.h (null)\n//unix/fcitx:mozc_response_parser.cc (null)\n//unix/fcitx:mozc_response_parser.h (null)"
  "//unix/fcitx:fcitx_key_util (914686d)"
  "//unix/fcitx:fcitx_key_util (914686d)" -> "//:compiler_msvc_like (914686d)"
  "//unix/fcitx:fcitx_key_util (914686d)" -> "//:macro (914686d)"
  "//unix/fcitx:fcitx_key_util (914686d)" -> "//base:port (914686d)"
  "//unix/fcitx:fcitx_key_util (914686d)" -> "//base:singleton (914686d)"
  "//unix/fcitx:fcitx_key_util (914686d)" -> "//base:vlog (914686d)"
  "//unix/fcitx:fcitx_key_util (914686d)" -> "//protocol:commands_cc_proto (914686d)"
  "//unix/fcitx:fcitx_key_util (914686d)" -> "//protocol:config_cc_proto (914686d)"
  "//unix/fcitx:fcitx_key_util (914686d)" -> "//unix/fcitx:fcitx_key_event_handler.cc (null)\n//unix/fcitx:fcitx_key_event_handler.h (null)\n//unix/fcitx:fcitx_key_translator.cc (null)\n//unix/fcitx:fcitx_key_translator.h (null)"
  "//unix/fcitx:fcitx_key_util (914686d)" -> "@com_google_absl//absl/log:check (914686d)"
  "//unix/fcitx:fcitx_key_util (914686d)" -> "@bazel_tools//tools:host_platform (914686d)\n@bazel_tools//tools:host_platform (ab8cba1)"
  "//unix/fcitx:fcitx_key_util (914686d)" -> "@bazel_tools//tools/cpp:current_cc_toolchain (914686d)"
  "//unix/fcitx:fcitx_key_util (914686d)" -> "@bazel_tools//tools/cpp:toolchain_type (914686d)"
  "//unix/fcitx:fcitx_key_util (914686d)" -> "@bazel_tools//tools/def_parser:def_parser (f9c3c74)"
  "//unix/fcitx:fcitx_key_util (914686d)" -> "@@bazel_tools~cc_configure_extension~local_config_cc//:cc-compiler-k8 (914686d)"
  "//unix/fcitx:fcitx_key_util (914686d)" -> "@platforms//host:host (ab8cba1)"
  "//unix/fcitx:fcitx_key_event_handler.cc (null)\n//unix/fcitx:fcitx_key_event_handler.h (null)\n//unix/fcitx:fcitx_key_translator.cc (null)\n//unix/fcitx:fcitx_key_translator.h (null)"
  "//unix/fcitx:eim.cc (null)"

@phoepsilonix
Copy link
Author

<fcitx/ime.h> referenced by unix/fcitx/fcitx_key_translator.h cannot be found, but is resolved by -I/usr/include.

@phoepsilonix
Copy link
Author

.cache/bazel/_bazel_phoepsilonix/a422ec5eb6e7740277eb4ee8963a4986/external/_main~_repo_rules~fcitx/BUILD.bazel

package(
    default_visibility = ["//visibility:public"],
)

cc_library(
    name = "fcitx",
    hdrs = glob([
        "/**"
    ]),
    copts = [
        ""
    ],
    includes = [
        ""
    ],
    linkopts = [
        ""
    ],
)

exports_files(glob(["libexec/*"]))

@phoepsilonix
Copy link
Author

phoepsilonix commented Oct 26, 2024

It seems that the absolute path here is the problem. Can this be avoided by setting pkg-config or the BUILD file?

    hdrs = glob([
        "/**"
    ]),

@phoepsilonix
Copy link
Author

phoepsilonix commented Oct 26, 2024

This may not be possible without modifying bazel/pkg_config_repository.bzl.
pkg-config --cflags-only-I fcitx usually has no output. Therefore, the script below does not have includes or items, so hdrs starts with the absolute path /**.This causes bazel to throw an error.

def _pkg_config_repository_impl(repo_ctx):
    includes = _exec_pkg_config(repo_ctx, "--cflags-only-I")
    includes = [item[len("-I/"):] for item in includes]
    _symlinks(repo_ctx, includes)
    data = {
        # In bzlmod, repo_ctx.attr.name has a prefix like "_main~_repo_rules~ibus".
        # Note also that Bazel 8.0+ uses "+" instead of "~".
        # https://github.com/bazelbuild/bazel/issues/23127
        "name": repo_ctx.attr.name.replace("~", "+").split("+")[-1],
        "hdrs": _make_strlist([item + "/**" for item in includes]),
        "copts": _make_strlist(_exec_pkg_config(repo_ctx, "--cflags-only-other")),
        "includes": _make_strlist(includes),
        "linkopts": _make_strlist(_exec_pkg_config(repo_ctx, "--libs-only-l")),
    }
    build_file_data = BUILD_TEMPLATE.format(**data)

@phoepsilonix
Copy link
Author

phoepsilonix commented Oct 26, 2024

Here are some workarounds I can think of.

Workaround 1
First add -I/usr/include/fcitx as a dummy to the fcitx.pc file for pkg-config.
Bazel will recognize usr/include/fcitx and enable @fcitx as a module.

cat /usr/lib/pkgconfig/fcitx.pc 
prefix=/usr
exec_prefix=/usr
libdir=/usr/lib
includedir=/usr/include

Name: fcitx
Description: Fcitx - Free Chinese Input Toy of X
Version: 4.2.9.9
Requires: fcitx-config
Cflags: -I/usr/include/fcitx -I/usr/include
Libs: -L/usr/lib -lfcitx-core

Workaround 2
Create a separate scripts for fcitx that is equivalent to bazel/pkg_config_repository.bzl.

diff --git a/src/MODULE.bazel b/src/MODULE.bazel
index df10ba2..7ce536f 100644
--- a/src/MODULE.bazel
+++ b/src/MODULE.bazel
@@ -128,7 +128,11 @@ pkg_config_repository(
 )
 
 # Fcitx
-pkg_config_repository(
+pkg_config_repository_fcitx = use_repo_rule(
+    "@//bazel:pkg_config_repository_fcitx.bzl",
+    "pkg_config_repository",
+)
+pkg_config_repository_fcitx(
     name = "fcitx",
     packages = ["fcitx"],
 )
diff --git a/src/bazel/pkg_config_repository_fcitx.bzl b/src/bazel/pkg_config_repository_fcitx.bzl
new file mode 100644
index 0000000..693e8ae
--- /dev/null
+++ b/src/bazel/pkg_config_repository_fcitx.bzl
@@ -0,0 +1,142 @@
+# Copyright 2010-2021, Google Inc.
+# All rights reserved.
+#
+# Redistribution and use in source and binary forms, with or without
+# modification, are permitted provided that the following conditions are
+# met:
+#
+#     * Redistributions of source code must retain the above copyright
+# notice, this list of conditions and the following disclaimer.
+#     * Redistributions in binary form must reproduce the above
+# copyright notice, this list of conditions and the following disclaimer
+# in the documentation and/or other materials provided with the
+# distribution.
+#     * Neither the name of Google Inc. nor the names of its
+# contributors may be used to endorse or promote products derived from
+# this software without specific prior written permission.
+#
+# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+"""Repository rule for Linux libraries configured by `pkg-config`.
+
+Note, this rule supports only necessary functionaries of pkg-config for Mozc.
+Generated `BUILD.bazel` is available at `bazel-src/external/<repository_name>`.
+
+## Example of usage
+```:WORKSPACE.bazel
+pkg_config_repository(
+  name = "ibus",
+  packages = ["glib-2.0", "gobject-2.0", "ibus-1.0"],
+)
+```
+
+```:BUILD.bazel
+cc_library(
+    name = "ibus_client",
+    deps = [
+        "@ibus//:ibus",
+        ...
+    ],
+    ...
+)
+```
+"""
+
+BUILD_TEMPLATE = """
+package(
+    default_visibility = ["//visibility:public"],
+)
+
+cc_library(
+    name = "{name}",
+    hdrs = glob([
+        {hdrs}
+    ]),
+    copts = [
+        {copts}
+    ],
+    includes = [
+        {includes}
+    ],
+    linkopts = [
+        {linkopts}
+    ],
+)
+"""
+
+EXPORTS_FILES_TEMPLATE = """
+exports_files(glob(["libexec/*"]))
+"""
+
+def _exec_pkg_config(repo_ctx, flag):
+    binary = repo_ctx.which("pkg-config")
+    if not binary:
+        # Using print is not recommended, but this will be a clue to debug build errors in
+        # the case of pkg-config is not found.
+        print("pkg-config is not found")  # buildifier: disable=print
+        return []
+    result = repo_ctx.execute([binary, flag] + repo_ctx.attr.packages)
+    items = result.stdout.strip().split(" ")
+    uniq_items = sorted({key: None for key in items}.keys())
+    return uniq_items
+
+def _make_strlist(list):
+    return "\"" + "\",\n        \"".join(list) + "\""
+
+def _symlinks(repo_ctx, paths):
+    for path in paths:
+        if repo_ctx.path(path).exists:
+            continue
+        repo_ctx.symlink("/" + path, path)
+
+def ensure_fcitx_include(packages, includes):
+    if packages == ["fcitx"]:
+        if not includes:
+            return ['-I/usr/include/fcitx', '-I/usr/include']
+        elif not includes[0]:
+            return ['-I/usr/include/fcitx', '-I/usr/include'] + includes[1:]
+    return includes
+
+def _pkg_config_repository_impl(repo_ctx):
+    includes = _exec_pkg_config(repo_ctx, "--cflags-only-I")
+    if repo_ctx.attr.packages == ["fcitx"]:
+        includes = ensure_fcitx_include(repo_ctx.attr.packages, includes)
+    includes = [item[len("-I/"):] for item in includes]
+    _symlinks(repo_ctx, includes)
+    data = {
+        # In bzlmod, repo_ctx.attr.name has a prefix like "_main~_repo_rules~ibus".
+        # Note also that Bazel 8.0+ uses "+" instead of "~".
+        # https://github.com/bazelbuild/bazel/issues/23127
+        "name": repo_ctx.attr.name.replace("~", "+").split("+")[-1],
+        "hdrs": _make_strlist([item + "/**" for item in includes]),
+        "copts": _make_strlist(_exec_pkg_config(repo_ctx, "--cflags-only-other")),
+        "includes": _make_strlist(includes),
+        "linkopts": _make_strlist(_exec_pkg_config(repo_ctx, "--libs-only-l")),
+    }
+    build_file_data = BUILD_TEMPLATE.format(**data)
+
+    libexecdir = _exec_pkg_config(repo_ctx, "--variable=libexecdir")
+    if len(libexecdir) == 1:
+        repo_ctx.symlink(libexecdir[0], "libexec")
+        build_file_data += EXPORTS_FILES_TEMPLATE
+
+    repo_ctx.file("BUILD.bazel", build_file_data)
+
+pkg_config_repository = repository_rule(
+    implementation = _pkg_config_repository_impl,
+    configure = True,
+    local = True,
+    attrs = {
+        "packages": attr.string_list(),
+    },
+)

Workaround 3
Modify bazel/pkg_config_repository.bzl

diff --git a/src/bazel/pkg_config_repository.bzl b/src/bazel/pkg_config_repository.bzl
index 8d46e01..693e8ae 100644
--- a/src/bazel/pkg_config_repository.bzl
+++ b/src/bazel/pkg_config_repository.bzl
@@ -99,8 +99,18 @@ def _symlinks(repo_ctx, paths):
             continue
         repo_ctx.symlink("/" + path, path)
 
+def ensure_fcitx_include(packages, includes):
+    if packages == ["fcitx"]:
+        if not includes:
+            return ['-I/usr/include/fcitx', '-I/usr/include']
+        elif not includes[0]:
+            return ['-I/usr/include/fcitx', '-I/usr/include'] + includes[1:]
+    return includes
+
 def _pkg_config_repository_impl(repo_ctx):
     includes = _exec_pkg_config(repo_ctx, "--cflags-only-I")
+    if repo_ctx.attr.packages == ["fcitx"]:
+        includes = ensure_fcitx_include(repo_ctx.attr.packages, includes)
     includes = [item[len("-I/"):] for item in includes]
     _symlinks(repo_ctx, includes)
     data = {

@phoepsilonix
Copy link
Author

phoepsilonix commented Oct 26, 2024

It seems we can make Bazel recognize @fcitx using one of these three methods.
Do you have a preferred approach?

phoepsilonix added a commit to TachiBanaHime/fcitx-mozc that referenced this pull request Oct 26, 2024
  * add build_fcitx_bazel

  * This is a fix to enable fcitx-mozc to be built with bazel.
  * add build_fcitx_bazel

  * This is a fix to enable fcitx-mozc to be built with bazel.
@phoepsilonix
Copy link
Author

If google#1095 is approved for merging, this (#68) will no longer be necessary.

@wengxt
Copy link
Member

wengxt commented Oct 26, 2024

Ok, it seems that its some issue with /usr/include

So we can try something else for fcitx anyway.

fcitx5 is not affected since it has an additonal layer of directory

@wengxt
Copy link
Member

wengxt commented Oct 26, 2024

we can go back to the old way (non pkg-config) to define fcitx repo, if that's the easiest solution

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants