Skip to content

Commit

Permalink
Use nix-provided gRPC on Unix-like OSs (digital-asset#11031)
Browse files Browse the repository at this point in the history
* Use nix-provided gRPC on Unix-like OSs

This fixes the da-ghcid command on macOS and Linux.

Unfortunately on macos at least on CI it still seems to segfault when
trying to run damlc in ghci. However, at least typechecking works
so this is clearly progress.

changelog_begin
changelog_end
  • Loading branch information
akrmn authored Oct 12, 2021
1 parent 578fe7f commit 0de7b2e
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 278 deletions.
104 changes: 23 additions & 81 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,6 @@ nixpkgs_package(
attribute_path = "curl",
nix_file = "//nix:bazel.nix",
nix_file_deps = common_nix_file_deps,
# Remove once we upgrade to Bazel >=3.0. Until then `nix-build` output
# confuses the JAR query in `daml-sdk-head`.
quiet = True,
repositories = dev_env_nix_repos,
)

Expand All @@ -167,9 +164,6 @@ nixpkgs_package(
fail_not_supported = False,
nix_file = "//nix:bazel.nix",
nix_file_deps = common_nix_file_deps,
# Remove once we upgrade to Bazel >=3.0. Until then `nix-build` output
# confuses the JAR query in `daml-sdk-head`.
quiet = True,
repositories = dev_env_nix_repos,
)

Expand All @@ -190,9 +184,6 @@ nixpkgs_package(
attribute_path = "patchelf",
nix_file = "//nix:bazel.nix",
nix_file_deps = common_nix_file_deps,
# Remove once we upgrade to Bazel >=3.0. Until then `nix-build` output
# confuses the JAR query in `daml-sdk-head`.
quiet = True,
repositories = dev_env_nix_repos,
)

Expand All @@ -202,9 +193,6 @@ nixpkgs_package(
attribute_path = "netcat-gnu",
nix_file = "//nix:bazel.nix",
nix_file_deps = common_nix_file_deps,
# Remove once we upgrade to Bazel >=3.0. Until then `nix-build` output
# confuses the JAR query in `daml-sdk-head`.
quiet = True,
repositories = dev_env_nix_repos,
)

Expand All @@ -225,9 +213,6 @@ nixpkgs_package(
fail_not_supported = False,
nix_file = "//nix:bazel.nix",
nix_file_deps = common_nix_file_deps,
# Remove once we upgrade to Bazel >=3.0. Until then `nix-build` output
# confuses the JAR query in `daml-sdk-head`.
quiet = True,
repositories = dev_env_nix_repos,
)

Expand Down Expand Up @@ -292,9 +277,6 @@ nixpkgs_package(
fail_not_supported = False,
nix_file = "//nix:bazel.nix",
nix_file_deps = common_nix_file_deps,
# Remove once we upgrade to Bazel >=3.0. Until then `nix-build` output
# confuses the JAR query in `daml-sdk-head`.
quiet = True,
repositories = dev_env_nix_repos,
)

Expand Down Expand Up @@ -330,9 +312,6 @@ nixpkgs_package(
attribute_path = "gawk",
nix_file = "//nix:bazel.nix",
nix_file_deps = common_nix_file_deps,
# Remove once we upgrade to Bazel >=3.0. Until then `nix-build` output
# confuses the JAR query in `daml-sdk-head`.
quiet = True,
repositories = dev_env_nix_repos,
)

Expand All @@ -341,9 +320,6 @@ nixpkgs_package(
attribute_path = "coreutils",
nix_file = "//nix:bazel.nix",
nix_file_deps = common_nix_file_deps,
# Remove once we upgrade to Bazel >=3.0. Until then `nix-build` output
# confuses the JAR query in `daml-sdk-head`.
quiet = True,
repositories = dev_env_nix_repos,
)

Expand All @@ -352,9 +328,6 @@ nixpkgs_package(
attribute_path = "grpcurl",
nix_file = "//nix:bazel.nix",
nix_file_deps = common_nix_file_deps,
# Remove once we upgrade to Bazel >=3.0. Until then `nix-build` output
# confuses the JAR query in `daml-sdk-head`.
quiet = True,
repositories = dev_env_nix_repos,
)

Expand All @@ -363,9 +336,6 @@ nixpkgs_package(
attribute_path = "hlint",
nix_file = "//nix:bazel.nix",
nix_file_deps = common_nix_file_deps,
# Remove once we upgrade to Bazel >=3.0. Until then `nix-build` output
# confuses the JAR query in `daml-sdk-head`.
quiet = True,
repositories = dev_env_nix_repos,
)

Expand All @@ -375,9 +345,6 @@ nixpkgs_package(
fail_not_supported = False,
nix_file = "//nix:bazel.nix",
nix_file_deps = common_nix_file_deps,
# Remove once we upgrade to Bazel >=3.0. Until then `nix-build` output
# confuses the JAR query in `daml-sdk-head`.
quiet = True,
repositories = dev_env_nix_repos,
)

Expand All @@ -386,9 +353,6 @@ nixpkgs_package(
attribute_path = "jekyll",
nix_file = "//nix:bazel.nix",
nix_file_deps = common_nix_file_deps,
# Remove once we upgrade to Bazel >=3.0. Until then `nix-build` output
# confuses the JAR query in `daml-sdk-head`.
quiet = True,
repositories = dev_env_nix_repos,
)

Expand All @@ -413,9 +377,6 @@ filegroup(
""",
nix_file = "//nix:bazel.nix",
nix_file_deps = common_nix_file_deps,
# Remove once we upgrade to Bazel >=3.0. Until then `nix-build` output
# confuses the JAR query in `daml-sdk-head`.
quiet = True,
repositories = dev_env_nix_repos,
) if is_linux else None

Expand All @@ -429,9 +390,6 @@ exports_files(glob(["lib/**/*"]))
""",
nix_file = "//nix:bazel.nix",
nix_file_deps = common_nix_file_deps,
# Remove once we upgrade to Bazel >=3.0. Until then `nix-build` output
# confuses the JAR query in `daml-sdk-head`.
quiet = True,
repositories = dev_env_nix_repos,
) if not is_windows else None

Expand Down Expand Up @@ -492,9 +450,6 @@ nixpkgs_package(
fail_not_supported = False,
nix_file = "//nix:bazel.nix",
nix_file_deps = common_nix_file_deps,
# Remove once we upgrade to Bazel >=3.0. Until then `nix-build` output
# confuses the JAR query in `daml-sdk-head`.
quiet = True,
repositories = dev_env_nix_repos,
)

Expand All @@ -516,9 +471,6 @@ nixpkgs_package(
fail_not_supported = False,
nix_file = "//nix:bazel.nix",
nix_file_deps = common_nix_file_deps,
# Remove once we upgrade to Bazel >=3.0. Until then `nix-build` output
# confuses the JAR query in `daml-sdk-head`.
quiet = True,
repositories = dev_env_nix_repos,
)

Expand All @@ -530,9 +482,6 @@ nixpkgs_package(
fail_not_supported = False,
nix_file = "//nix:bazel.nix",
nix_file_deps = common_nix_file_deps,
# Remove once we upgrade to Bazel >=3.0. Until then `nix-build` output
# confuses the JAR query in `daml-sdk-head`.
quiet = True,
repositories = dev_env_nix_repos,
)

Expand All @@ -542,9 +491,6 @@ nixpkgs_package(
attribute_path = "sass",
nix_file = "//nix:bazel.nix",
nix_file_deps = common_nix_file_deps,
# Remove once we upgrade to Bazel >=3.0. Until then `nix-build` output
# confuses the JAR query in `daml-sdk-head`.
quiet = True,
repositories = dev_env_nix_repos,
)

Expand All @@ -554,9 +500,6 @@ nixpkgs_package(
attribute_path = "texlive",
nix_file = "//nix:bazel.nix",
nix_file_deps = common_nix_file_deps,
# Remove once we upgrade to Bazel >=3.0. Until then `nix-build` output
# confuses the JAR query in `daml-sdk-head`.
quiet = True,
repositories = dev_env_nix_repos,
)

Expand All @@ -566,9 +509,6 @@ nixpkgs_package(
attribute_path = "sphinx183-exts",
nix_file = "//nix:bazel.nix",
nix_file_deps = common_nix_file_deps,
# Remove once we upgrade to Bazel >=3.0. Until then `nix-build` output
# confuses the JAR query in `daml-sdk-head`.
quiet = True,
repositories = dev_env_nix_repos,
)

Expand All @@ -578,9 +518,6 @@ nixpkgs_package(
attribute_path = "imagemagick",
nix_file = "//nix:bazel.nix",
nix_file_deps = common_nix_file_deps,
# Remove once we upgrade to Bazel >=3.0. Until then `nix-build` output
# confuses the JAR query in `daml-sdk-head`.
quiet = True,
repositories = dev_env_nix_repos,
)

Expand All @@ -591,9 +528,6 @@ nixpkgs_package(
fail_not_supported = False,
nix_file = "//nix:bazel.nix",
nix_file_deps = common_nix_file_deps,
# Remove once we upgrade to Bazel >=3.0. Until then `nix-build` output
# confuses the JAR query in `daml-sdk-head`.
quiet = True,
repositories = dev_env_nix_repos,
)

Expand All @@ -604,9 +538,6 @@ nixpkgs_package(
fail_not_supported = False,
nix_file = "//nix:bazel.nix",
nix_file_deps = common_nix_file_deps,
# Remove once we upgrade to Bazel >=3.0. Until then `nix-build` output
# confuses the JAR query in `daml-sdk-head`.
quiet = True,
repositories = dev_env_nix_repos,
)

Expand All @@ -633,9 +564,6 @@ nixpkgs_package(
attribute_path = "scala_{}".format(scala_major_version_suffix),
nix_file = "//nix:bazel.nix",
nix_file_deps = common_nix_file_deps,
# Remove once we upgrade to Bazel >=3.0. Until then `nix-build` output
# confuses the JAR query in `daml-sdk-head`.
quiet = True,
repositories = dev_env_nix_repos,
)

Expand Down Expand Up @@ -685,9 +613,6 @@ nixpkgs_package(
""",
nix_file = "//nix:bazel.nix",
nix_file_deps = common_nix_file_deps,
# Remove once we upgrade to Bazel >=3.0. Until then `nix-build` output
# confuses the JAR query in `daml-sdk-head`.
quiet = True,
repositories = dev_env_nix_repos,
)

Expand Down Expand Up @@ -894,15 +819,35 @@ load("@com_github_bazelbuild_buildtools//buildifier:deps.bzl", "buildifier_depen

buildifier_dependencies()

nixpkgs_package(
name = "grpc_nix",
attribute_path = "grpc",
build_file_content = """
load("@os_info//:os_info.bzl", "is_linux")
cc_library(
name = "grpc_lib",
srcs = [":lib/libgrpc.so", ":lib/libgrpc.so.18", ":lib/libgrpc.so.18.0.0", ":lib/libgpr.so", ":lib/libgpr.so.18", ":lib/libgpr.so.18.0.0"] if is_linux else [":lib/libgrpc.dylib", ":lib/libgpr.dylib"],
visibility = ["//visibility:public"],
hdrs = [":include"],
includes = ["include"],
)
filegroup(
name = "grpc_file",
srcs = glob(["lib/*"]),
visibility = ["//visibility:public"],
)
""",
nix_file = "//nix:bazel.nix",
nix_file_deps = common_nix_file_deps,
repositories = dev_env_nix_repos,
)

nixpkgs_package(
name = "postgresql_nix",
attribute_path = "postgresql_9_6",
fail_not_supported = False,
nix_file = "//nix:bazel.nix",
nix_file_deps = common_nix_file_deps,
# Remove once we upgrade to Bazel >=3.0. Until then `nix-build` output
# confuses the JAR query in `daml-sdk-head`.
quiet = True,
repositories = dev_env_nix_repos,
)

Expand All @@ -912,9 +857,6 @@ nixpkgs_package(
fail_not_supported = False,
nix_file = "//nix:bazel.nix",
nix_file_deps = common_nix_file_deps,
# Remove once we upgrade to Bazel >=3.0. Until then `nix-build` output
# confuses the JAR query in `daml-sdk-head`.
quiet = True,
repositories = dev_env_nix_repos,
) if not is_windows else None

Expand Down
41 changes: 37 additions & 4 deletions bazel-haskell-deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

load("@bazel_skylib//lib:dicts.bzl", "dicts")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
load("@os_info//:os_info.bzl", "is_windows")
load("@os_info//:os_info.bzl", "is_linux", "is_windows")
load("@dadew//:dadew.bzl", "dadew_tool_home")
load("@rules_haskell//haskell:cabal.bzl", "stack_snapshot")

Expand Down Expand Up @@ -120,6 +120,9 @@ haskell_library(
urls = ["https://github.com/digital-asset/daml-ghcide/archive/%s.tar.gz" % GHCIDE_REV],
)

cbit_dep = ":fat_cbits" if is_windows else ":needed-cbits-clib" if is_linux else ":cbits"
grpc_dep = "@com_github_grpc_grpc//:grpc" if is_windows else "@grpc_nix//:grpc_lib"

http_archive(
name = "grpc_haskell_core",
build_file_content = """
Expand All @@ -146,10 +149,40 @@ c2hs_suite(
compiler_flags = ["-XCPP", "-Wno-unused-imports", "-Wno-unused-record-wildcards"],
visibility = ["//visibility:public"],
deps = [
":fat_cbits",
"{cbit_dep}",
],
)
cc_library(
name = "needed-cbits-clib",
srcs = [":libneeded-cbits.so"],
deps = ["{grpc_dep}"],
hdrs = glob(["include/*.h"]),
includes = ["include/"],
)
# Bazel produces cbits without NEEDED entries which makes
# ghci unhappy. We cannot use fat_cbits on the nix-built grpc
# since it lacks static libs but we can patchelf the cbits to add
# the NEEDED entry.
# Apparently this is not needed on macos for whatever reason and patchelf
# doesn’t work there anyway so we only use it on Linux.
genrule(
name = "needed-cbits",
srcs = [":cbits", "@grpc_nix//:grpc_file"],
outs = ["libneeded-cbits.so"],
tools = ["@patchelf_nix//:bin/patchelf"],
cmd = '''
set -eou pipefail
# We get 3 libs. We want the shared lib which comes last.
CBITS=$$(echo $(locations :cbits) | cut -f 3 -d ' ')
OLD_RPATH=$$($(location @patchelf_nix//:bin/patchelf) --print-rpath $$CBITS)
GRPC_RPATH=$$(dirname $$(readlink -f $$(echo $(locations @grpc_nix//:grpc_file) | cut -f 1 -d ' ')))
$(location @patchelf_nix//:bin/patchelf) $$CBITS --add-needed libgrpc.so.18 --output $(location libneeded-cbits.so)
$(location @patchelf_nix//:bin/patchelf) $(location libneeded-cbits.so) --set-rpath "$$OLD_RPATH:$$GRPC_RPATH"
'''
)
fat_cc_library(
name = "fat_cbits",
input_lib = "cbits",
Expand All @@ -160,10 +193,10 @@ cc_library(
hdrs = glob(["include/*.h"]),
includes = ["include/"],
deps = [
"@com_github_grpc_grpc//:grpc",
"{grpc_dep}",
]
)
""",
""".format(cbit_dep = cbit_dep, grpc_dep = grpc_dep),
patch_args = ["-p1"],
patches = [
"@com_github_digital_asset_daml//bazel_tools:grpc-haskell-core-cpp-options.patch",
Expand Down
Loading

0 comments on commit 0de7b2e

Please sign in to comment.