diff --git a/.bazelrc b/.bazelrc index 52a39b859fdd..ad7b5b1e40ee 100644 --- a/.bazelrc +++ b/.bazelrc @@ -97,8 +97,8 @@ build:linux --action_env=LOCALE_ARCHIVE build:windows --action_env=JAVA_HOME # Tell bazel to use the nixpkgs Haskell toolchain on Linux and Darwin -build:linux --host_platform=@rules_haskell//haskell/platforms:linux_x86_64_nixpkgs -build:darwin --host_platform=@rules_haskell//haskell/platforms:darwin_x86_64_nixpkgs +build:linux --host_platform=@io_tweag_rules_nixpkgs//nixpkgs/platforms:host +build:darwin --host_platform=@io_tweag_rules_nixpkgs//nixpkgs/platforms:host # and GHC's gcc on Windows build:windows --crosstool_top=@rules_haskell_ghc_windows_amd64//:cc_toolchain diff --git a/WORKSPACE b/WORKSPACE index b4acddf3a206..82e8a47aee81 100644 --- a/WORKSPACE +++ b/WORKSPACE @@ -589,38 +589,17 @@ nixpkgs_java_configure( ) if not is_windows else None # rules_go used here to compile a wrapper around the protoc-gen-scala plugin -load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies") +load("@io_tweag_rules_nixpkgs//nixpkgs:toolchains/go.bzl", "nixpkgs_go_configure") -nixpkgs_package( - name = "go_nix", - attribute_path = "go", - build_file_content = """ - filegroup( - name = "sdk", - srcs = glob(["share/go/**"]), - visibility = ["//visibility:public"], - ) - """, - nix_file = "//nix:bazel.nix", +nixpkgs_go_configure( + nix_file = "//nix:bazel-go-toolchain.nix", nix_file_deps = common_nix_file_deps, repositories = dev_env_nix_repos, -) - -# A repository that generates the Go SDK imports, see -# ./bazel_tools/go_sdk/README.md -local_repository( - name = "go_sdk_repo", - path = "bazel_tools/go_sdk", -) +) if not is_windows else None -load("@io_bazel_rules_go//go:deps.bzl", "go_wrap_sdk") +load("@io_bazel_rules_go//go:deps.bzl", "go_register_toolchains") -# On Nix platforms we use the Nix provided Go SDK, on Windows we let Bazel pull -# an upstream one. -go_wrap_sdk( - name = "go_sdk", - root_file = "@go_nix//:share/go/ROOT", -) if not is_windows else None +go_register_toolchains(version = "1.16.9") if is_windows else None # gazelle:repo bazel_gazelle load("@bazel_gazelle//:deps.bzl", "gazelle_dependencies") @@ -629,9 +608,9 @@ load("//:go_deps.bzl", "go_deps") # gazelle:repository_macro go_deps.bzl%go_deps go_deps() -go_rules_dependencies() +load("@io_bazel_rules_go//go:deps.bzl", "go_rules_dependencies") -go_register_toolchains() if not is_windows else go_register_toolchains(version = "1.16.9") +go_rules_dependencies() gazelle_dependencies() @@ -721,12 +700,13 @@ dev_env_tool( load("@build_bazel_rules_nodejs//:index.bzl", "node_repositories", "yarn_install") node_repositories( - package_json = ["//:package.json"], # Using `dev_env_tool` introduces an additional layer of symlink # indirection. Bazel doesn't track dependencies through symbolic links. # Occasionally, this can cause build failures on CI if a build is not # invalidated despite a change of an original source. To avoid such issues # we use the `nixpkgs_package` directly. + node_version = "16.13.0", + package_json = ["//:package.json"], vendored_node = "@nodejs_dev_env" if is_windows else "@node_nix", ) diff --git a/bazel-haskell-deps.bzl b/bazel-haskell-deps.bzl index e448b68a2eb8..45c371dc7916 100644 --- a/bazel-haskell-deps.bzl +++ b/bazel-haskell-deps.bzl @@ -57,6 +57,7 @@ haskell_cabal_library( version = packages["lsp-types"].version, srcs = glob(["**"]), deps = packages["lsp-types"].deps, + haddock = False, visibility = ["//visibility:public"], )""", patch_args = ["-p1"], diff --git a/bazel_tools/fat_cc_library.bzl b/bazel_tools/fat_cc_library.bzl index beff031cba55..1ef7fa02c8f1 100644 --- a/bazel_tools/fat_cc_library.bzl +++ b/bazel_tools/fat_cc_library.bzl @@ -53,7 +53,7 @@ def _fat_cc_library_impl(ctx): [f.path for f in static_libs] + ctx.attr.no_whole_archive_flag + # Some libs seems to depend on libstdc++ implicitely - ["-lstdc++"] + + (["-lc++", "-lc++abi"] if is_darwin else ["-lstdc++"]) + (["-framework", "CoreFoundation"] if is_darwin else []) + # On Windows we have some extra deps. (["-lws2_32"] if is_windows else []), diff --git a/bazel_tools/haskell-arm-m1.patch b/bazel_tools/haskell-arm-m1.patch new file mode 100644 index 000000000000..6771fb33e6d8 --- /dev/null +++ b/bazel_tools/haskell-arm-m1.patch @@ -0,0 +1,197 @@ +diff --git a/haskell/cabal.bzl b/haskell/cabal.bzl +index 6be79b0d..1e324ec5 100644 +--- a/haskell/cabal.bzl ++++ b/haskell/cabal.bzl +@@ -171,10 +171,11 @@ def _cabal_toolchain_info(hs, cc, workspace_name, runghc): + runghc = runghc.path, + ar = ar, + cc = cc.tools.cc, ++ ld = cc.tools.ld, + strip = cc.tools.strip, + is_windows = hs.toolchain.is_windows, + workspace = workspace_name, +- ghc_cc_args = ghc_cc_program_args(hs, "$CC"), ++ ghc_cc_args = ghc_cc_program_args(hs, "$CC", "$LD"), + ) + + def _prepare_cabal_inputs( +diff --git a/haskell/cc.bzl b/haskell/cc.bzl +index 3726133c..d59f84f7 100644 +--- a/haskell/cc.bzl ++++ b/haskell/cc.bzl +@@ -186,11 +186,12 @@ def cc_interop_info(ctx, override_cc_toolchain = None): + ]).linking_context.linker_inputs.to_list() for lib in li.libraries], + ) + +-def ghc_cc_program_args(hs, cc): ++def ghc_cc_program_args(hs, cc, ld): + """Returns the -pgm* flags required to override cc. + + Args: + cc: string, path to the C compiler (cc_wrapper). ++ ld: string, path to the linker (ld). + + Returns: + list of string, GHC arguments. +@@ -220,4 +221,11 @@ def ghc_cc_program_args(hs, cc): + ] + if hs.toolchain.numeric_version >= [8, 10, 3]: + args.append("-pgmc-supports-no-pie") ++ if hs.toolchain.numeric_version >= [9, 0, 1]: ++ args.extend([ ++ "-pgmlm", ++ ld, ++ "-optlm", ++ "-r", ++ ]) + return args +diff --git a/haskell/doctest.bzl b/haskell/doctest.bzl +index 1cd3f675..f7a2200a 100644 +--- a/haskell/doctest.bzl ++++ b/haskell/doctest.bzl +@@ -98,7 +98,7 @@ def _haskell_doctest_single(target, ctx): + ctx, + override_cc_toolchain = hs.tools_config.maybe_exec_cc_toolchain, + ) +- args.add_all(ghc_cc_program_args(hs, cc.tools.cc)) ++ args.add_all(ghc_cc_program_args(hs, cc.tools.cc, cc.tools.ld)) + + doctest_log = ctx.actions.declare_file( + "doctest-log-" + ctx.label.name + "-" + target.label.name, +diff --git a/haskell/nixpkgs.bzl b/haskell/nixpkgs.bzl +index 7ccead78..6692fe6b 100644 +--- a/haskell/nixpkgs.bzl ++++ b/haskell/nixpkgs.bzl +@@ -1,5 +1,6 @@ + """Workspace rules (Nixpkgs)""" + ++load("@bazel_tools//tools/cpp:lib_cc_configure.bzl", "get_cpu_value") + load( + "@io_tweag_rules_nixpkgs//nixpkgs:nixpkgs.bzl", + "nixpkgs_package", +@@ -145,15 +146,21 @@ def _ghc_nixpkgs_toolchain_impl(repository_ctx): + # platform. But they are important to state because Bazel + # toolchain resolution prefers other toolchains with more specific + # constraints otherwise. +- if repository_ctx.attr.target_constraints == [] and repository_ctx.attr.exec_constraints == []: +- target_constraints = ["@platforms//cpu:x86_64"] +- if repository_ctx.os.name == "linux": +- target_constraints.append("@platforms//os:linux") +- elif repository_ctx.os.name == "mac os x": +- target_constraints.append("@platforms//os:osx") +- exec_constraints = list(target_constraints) ++ cpu_value = get_cpu_value(repository_ctx) ++ cpu = { ++ "darwin": "@platforms//cpu:x86_64", ++ "darwin_arm64": "@platforms//cpu:arm64", ++ }.get(cpu_value, "@platforms//cpu:x86_64") ++ os = { ++ "darwin": "@platforms//os:osx", ++ "darwin_arm64": "@platforms//os:osx", ++ }.get(cpu_value, "@platforms//os:linux") ++ ++ if not repository_ctx.attr.target_constraints and not repository_ctx.attr.exec_constraints: ++ target_constraints = [cpu, os] ++ exec_constraints = target_constraints + else: +- target_constraints = repository_ctx.attr.target_constraints ++ target_constraints = list(repository_ctx.attr.target_constraints) + exec_constraints = list(repository_ctx.attr.exec_constraints) + + exec_constraints.append("@io_tweag_rules_nixpkgs//nixpkgs/constraints:support_nix") +diff --git a/haskell/private/cabal_wrapper.py b/haskell/private/cabal_wrapper.py +index 1ba677b3..b14135f9 100755 +--- a/haskell/private/cabal_wrapper.py ++++ b/haskell/private/cabal_wrapper.py +@@ -129,6 +129,7 @@ path_args = json_args["path_args"] + + ar = find_exe(toolchain_info["ar"]) + cc = find_exe(toolchain_info["cc"]) ++ld = find_exe(toolchain_info["ld"]) + strip = find_exe(toolchain_info["strip"]) + + def recache_db(): +@@ -261,10 +262,14 @@ with mkdtemp(distdir_prefix()) as distdir: + "--with-hsc2hs=" + hsc2hs, + "--with-ar=" + ar, + "--with-gcc=" + cc, ++ "--with-ld=" + ld, + "--with-strip=" + strip, + "--enable-deterministic", \ + ] + +- [ "--ghc-option=" + flag.replace("$CC", cc) for flag in toolchain_info["ghc_cc_args"] ] + ++ [ "--ghc-option=" + flag.replace("$CC", cc).replace("$LD", ld) for flag in toolchain_info["ghc_cc_args"] ] + ++ [ "--hsc2hs-option=-c" + cc, ++ "--hsc2hs-option=-l" + cc, ++ ] + + enable_relocatable_flags + \ + [ \ + # Make `--builddir` a relative path. Using an absolute path would +diff --git a/haskell/private/cc_wrapper.py.tpl b/haskell/private/cc_wrapper.py.tpl +index 311c0f96..9249f30b 100644 +--- a/haskell/private/cc_wrapper.py.tpl ++++ b/haskell/private/cc_wrapper.py.tpl +@@ -84,6 +84,7 @@ CC = os.environ.get("CC_WRAPPER_CC_PATH", "{:cc:}") + PLATFORM = os.environ.get("CC_WRAPPER_PLATFORM", "{:platform:}") + CPU = os.environ.get("CC_WRAPPER_CPU", "{:cpu:}") + INSTALL_NAME_TOOL = "/usr/bin/install_name_tool" ++CODESIGN = "/usr/bin/codesign" + OTOOL = "/usr/bin/otool" + + +@@ -938,6 +939,14 @@ def darwin_rewrite_load_commands(rewrites, output): + args.extend(["-change", old, os.path.join("@rpath", new)]) + if args: + subprocess.check_call([INSTALL_NAME_TOOL] + args + [output]) ++ # Resign the binary after patching it. ++ # This is necessary on MacOS Monterey on M1. ++ # The moving back and forth is necessary because the OS caches the signature. ++ # See this note from nixpkgs for reference: ++ # https://github.com/NixOS/nixpkgs/blob/5855ff74f511423e3e2646248598b3ffff229223/pkgs/os-specific/darwin/signing-utils/utils.sh#L1-L6 ++ os.rename(output, f"{output}.resign") ++ subprocess.check_call([CODESIGN] + ["-f", "-s", "-"] + [f"{output}.resign"]) ++ os.rename(f"{output}.resign", output) + + + # -------------------------------------------------------------------- +diff --git a/haskell/repl.bzl b/haskell/repl.bzl +index 6cc44499..0833ec92 100644 +--- a/haskell/repl.bzl ++++ b/haskell/repl.bzl +@@ -396,7 +396,12 @@ def _create_repl(hs, cc, posix, ctx, repl_info, output): + "$RULES_HASKELL_EXEC_ROOT", + hs.toolchain.cc_wrapper.executable.path, + ) +- args.extend(['"{}"'.format(arg) for arg in ghc_cc_program_args(hs, cc_path)]) ++ ld_path = paths.join( ++ "$RULES_HASKELL_EXEC_ROOT", ++ cc.ld_executable, ++ ) ++ args.extend(['"{}"'.format(arg) for arg in ghc_cc_program_args(hs, cc_path, ld_path)]) ++ + + # Load source files + # Force loading by source with `:add *...`. +@@ -518,7 +523,8 @@ def _create_hie_bios(hs, cc, posix, ctx, repl_info, path_prefix): + path_prefix = paths.join("", *path_prefix) + args, inputs = _compiler_flags_and_inputs(hs, cc, repl_info, path_prefix = path_prefix, static = True) + cc_path = paths.join(path_prefix, hs.toolchain.cc_wrapper.executable.path) +- args.extend(ghc_cc_program_args(hs, cc_path)) ++ ld_path = paths.join(path_prefix, cc.ld_executable) ++ args.extend(ghc_cc_program_args(hs, cc_path, ld_path)) + args.extend(hs.toolchain.ghcopts) + args.extend(repl_info.load_info.compiler_flags) + +diff --git a/haskell/toolchain.bzl b/haskell/toolchain.bzl +index 2cecc0b8..74f72163 100644 +--- a/haskell/toolchain.bzl ++++ b/haskell/toolchain.bzl +@@ -46,7 +46,7 @@ def _run_ghc(hs, cc, inputs, outputs, mnemonic, arguments, env, params_file = No + + # XXX: We should also tether Bazel's CC toolchain to GHC's, so that we can properly mix Bazel-compiled + # C libraries with Haskell targets. +- args.add_all(ghc_cc_program_args(hs, cc.tools.cc)) ++ args.add_all(ghc_cc_program_args(hs, cc.tools.cc, cc.tools.ld)) + + compile_flags_file = hs.actions.declare_file("compile_flags_%s_%s_%s" % (hs.name, extra_name, mnemonic)) + extra_args_file = hs.actions.declare_file("extra_args_%s_%s_%s" % (hs.name, extra_name, mnemonic)) diff --git a/bazel_tools/nixpkgs-disable-http2.patch b/bazel_tools/nixpkgs-disable-http2.patch index d5faeab8f2ad..d37a3a66581f 100644 --- a/bazel_tools/nixpkgs-disable-http2.patch +++ b/bazel_tools/nixpkgs-disable-http2.patch @@ -1,8 +1,8 @@ diff --git a/nixpkgs/nixpkgs.bzl b/nixpkgs/nixpkgs.bzl -index 666948e..8d99441 100644 +index 16fe59c..d590d44 100644 --- a/nixpkgs/nixpkgs.bzl +++ b/nixpkgs/nixpkgs.bzl -@@ -116,7 +116,7 @@ def _nixpkgs_package_impl(repository_ctx): +@@ -136,7 +136,7 @@ def _nixpkgs_package_impl(repository_ctx): "The NIX_PATH environment variable is not inherited." ) diff --git a/bazel_tools/os_info.bzl b/bazel_tools/os_info.bzl index f00abab37048..575fed3029f3 100644 --- a/bazel_tools/os_info.bzl +++ b/bazel_tools/os_info.bzl @@ -5,7 +5,8 @@ load("@bazel_tools//tools/cpp:lib_cc_configure.bzl", "get_cpu_value") _os_info_bzl_template = """ cpu_value = "{CPU_VALUE}" -is_darwin = cpu_value == "darwin" +is_darwin = cpu_value == "darwin" or cpu_value == "darwin_arm64" +is_darwin_arm64 = cpu_value == "darwin_arm64" is_linux = cpu_value == "k8" is_windows = cpu_value == "x64_windows" os_name = "macos" if is_darwin else "linux" if is_linux else "windows" @@ -13,6 +14,14 @@ os_name = "macos" if is_darwin else "linux" if is_linux else "windows" def _os_info_impl(repository_ctx): cpu = get_cpu_value(repository_ctx) + known_cpu_values = [ + "darwin", + "darwin_arm64", + "k8", + "x64_windows", + ] + if cpu not in known_cpu_values: + fail("Unknown OS type {}, expected one of {}".format(cpu, ", ".join(known_cpu_values))) os_info_substitutions = { "CPU_VALUE": cpu, } diff --git a/bazel_tools/packaging/package-app.sh b/bazel_tools/packaging/package-app.sh index 04129f8a8780..bcb219790efa 100755 --- a/bazel_tools/packaging/package-app.sh +++ b/bazel_tools/packaging/package-app.sh @@ -211,6 +211,14 @@ elif [[ "$(uname -s)" == "Darwin" ]]; then fi fi done + + # Resign the binary on MacOS. Requires moving the file back and forth + # because the OS caches the signatore. Refer to the following note in + # nixpkgs for details: + # https://github.com/NixOS/nixpkgs/blob/5855ff74f511423e3e2646248598b3ffff229223/pkgs/os-specific/darwin/signing-utils/utils.sh#L1-L6 + mv "$from_copied" "$from_copied.resign" + /usr/bin/codesign -f -s - "$from_copied.resign" + mv "$from_copied.resign" "$from_copied" } # Set the dynamic library load path to the relative lib/ directory. /usr/bin/install_name_tool -add_rpath "@loader_path/lib" $WORKDIR/$NAME/$NAME diff --git a/bazel_tools/rules-nixpkgs-arm.patch b/bazel_tools/rules-nixpkgs-arm.patch new file mode 100644 index 000000000000..622776152535 --- /dev/null +++ b/bazel_tools/rules-nixpkgs-arm.patch @@ -0,0 +1,223 @@ +diff --git a/README.md b/README.md +index e7d15ca..a1f7440 100644 +--- a/README.md ++++ b/README.md +@@ -1332,7 +1332,7 @@ Constraints for the target platform. + ### nixpkgs_sh_posix_configure + +
+-nixpkgs_sh_posix_configure(name, packages, kwargs)
++nixpkgs_sh_posix_configure(name, packages, exec_constraints, kwargs)
+ 
+ + Create a POSIX toolchain from nixpkgs. +@@ -1378,6 +1378,20 @@ default is ["stdenv.initialPath"] + + List of Nix attribute paths to draw Unix tools from. + ++

++ ++ ++ ++exec_constraints ++ ++ ++optional. ++default is None ++ ++

++ ++Constraints for the execution platform. ++ +

+ + +diff --git a/nixpkgs/nixpkgs.bzl b/nixpkgs/nixpkgs.bzl +index d590d44..01b4ba0 100644 +--- a/nixpkgs/nixpkgs.bzl ++++ b/nixpkgs/nixpkgs.bzl +@@ -11,7 +11,12 @@ load( + ) + load("@bazel_tools//tools/build_defs/repo:utils.bzl", "maybe") + load(":private/location_expansion.bzl", "expand_location") +-load(":private/constraints.bzl", "ensure_constraints") ++load( ++ ":private/constraints.bzl", ++ "default_constraints", ++ "ensure_constraints", ++ "ensure_constraints_pure", ++) + + def _nixpkgs_git_repository_impl(repository_ctx): + repository_ctx.file( +@@ -443,7 +448,7 @@ def _parse_cc_toolchain_info(content, filename): + + def _nixpkgs_cc_toolchain_config_impl(repository_ctx): + cpu_value = get_cpu_value(repository_ctx) +- darwin = cpu_value == "darwin" ++ darwin = cpu_value == "darwin" or cpu_value == "darwin_arm64" + + cc_toolchain_info_file = repository_ctx.path(repository_ctx.attr.cc_toolchain_info) + if not cc_toolchain_info_file.exists and not repository_ctx.attr.fail_not_supported: +@@ -1344,36 +1349,37 @@ create_posix_toolchain() + # should run. + + def _nixpkgs_sh_posix_toolchain_impl(repository_ctx): +- cpu = get_cpu_value(repository_ctx) ++ exec_constraints, _ = ensure_constraints_pure( ++ default_constraints = default_constraints(repository_ctx), ++ exec_constraints = repository_ctx.attr.exec_constraints, ++ ) + repository_ctx.file("BUILD", executable = False, content = """ + toolchain( + name = "nixpkgs_sh_posix_toolchain", + toolchain = "@{workspace}//:nixpkgs_sh_posix", + toolchain_type = "@rules_sh//sh/posix:toolchain_type", +- exec_compatible_with = [ +- "@platforms//cpu:x86_64", +- "@platforms//os:{os}", +- "@io_tweag_rules_nixpkgs//nixpkgs/constraints:support_nix", +- ], ++ exec_compatible_with = {exec_constraints}, + # Leaving the target constraints empty matter for cross-compilation. + # See Note [Target constraints for POSIX tools] + target_compatible_with = [], + ) + """.format( + workspace = repository_ctx.attr.workspace, +- os = {"darwin": "osx"}.get(cpu, "linux"), ++ exec_constraints = exec_constraints, + )) + + _nixpkgs_sh_posix_toolchain = repository_rule( + _nixpkgs_sh_posix_toolchain_impl, + attrs = { + "workspace": attr.string(), ++ "exec_constraints": attr.string_list(), + }, + ) + + def nixpkgs_sh_posix_configure( + name = "nixpkgs_sh_posix_config", + packages = ["stdenv.initialPath"], ++ exec_constraints = None, + **kwargs): + """Create a POSIX toolchain from nixpkgs. + +@@ -1387,6 +1393,7 @@ def nixpkgs_sh_posix_configure( + Args: + name: Name prefix for the generated repositories. + packages: List of Nix attribute paths to draw Unix tools from. ++ exec_constraints: Constraints for the execution platform. + nix_file_deps: See nixpkgs_package. + repositories: See nixpkgs_package. + repository: See nixpkgs_package. +@@ -1396,6 +1403,7 @@ def nixpkgs_sh_posix_configure( + nixpkgs_sh_posix_config( + name = name, + packages = packages, ++ exec_constraints = exec_constraints, + **kwargs + ) + +diff --git a/nixpkgs/private/constraints.bzl b/nixpkgs/private/constraints.bzl +index b35ec0b..fe353b7 100644 +--- a/nixpkgs/private/constraints.bzl ++++ b/nixpkgs/private/constraints.bzl +@@ -1,26 +1,66 @@ + load("@bazel_tools//tools/cpp:lib_cc_configure.bzl", "get_cpu_value") + +-def ensure_constraints(repository_ctx): ++def default_constraints(repository_ctx): ++ """Calculate the default CPU and OS constraints based on the host platform. ++ ++ Args: ++ repository_ctx: The repository context of the current repository rule. ++ ++ Returns: ++ A list containing the cpu and os constraints. ++ """ ++ cpu_value = get_cpu_value(repository_ctx) ++ cpu = { ++ "darwin": "@platforms//cpu:x86_64", ++ "darwin_arm64": "@platforms//cpu:arm64", ++ }.get(cpu_value, "@platforms//cpu:x86_64") ++ os = { ++ "darwin": "@platforms//os:osx", ++ "darwin_arm64": "@platforms//os:osx", ++ }.get(cpu_value, "@platforms//os:linux") ++ return [cpu, os] ++ ++def ensure_constraints_pure(default_constraints, target_constraints = [], exec_constraints = []): + """Build exec and target constraints for repository rules. + +- If these are user-provided, then they are passed through. Otherwise we build for x86_64 on the current OS. ++ If these are user-provided, then they are passed through. ++ Otherwise, use the provided default constraints. + In either case, exec_constraints always contain the support_nix constraint, so the toolchain can be rejected on non-Nix environments. + + Args: +- repository_ctx: The repository context of the current repository rule. ++ target_constraints: optional, User provided target_constraints. ++ exec_constraints: optional, User provided exec_constraints. ++ default_constraints: Fall-back constraints. + + Returns: + exec_constraints, The generated list of exec constraints + target_constraints, The generated list of target constraints + """ +- cpu = get_cpu_value(repository_ctx) +- os = {"darwin": "osx"}.get(cpu, "linux") +- if not repository_ctx.attr.target_constraints and not repository_ctx.attr.exec_constraints: +- target_constraints = ["@platforms//cpu:x86_64"] +- target_constraints.append("@platforms//os:{}".format(os)) ++ if not target_constraints and not exec_constraints: ++ target_constraints = default_constraints + exec_constraints = target_constraints + else: +- target_constraints = list(repository_ctx.attr.target_constraints) +- exec_constraints = list(repository_ctx.attr.exec_constraints) ++ target_constraints = list(target_constraints) ++ exec_constraints = list(exec_constraints) + exec_constraints.append("@io_tweag_rules_nixpkgs//nixpkgs/constraints:support_nix") + return exec_constraints, target_constraints ++ ++def ensure_constraints(repository_ctx): ++ """Build exec and target constraints for repository rules. ++ ++ If these are user-provided, then they are passed through. ++ Otherwise we build for the current CPU on the current OS, one of darwin-x86_64, darwin-arm64, or the default linux-x86_64. ++ In either case, exec_constraints always contain the support_nix constraint, so the toolchain can be rejected on non-Nix environments. ++ ++ Args: ++ repository_ctx: The repository context of the current repository rule. ++ ++ Returns: ++ exec_constraints, The generated list of exec constraints ++ target_constraints, The generated list of target constraints ++ """ ++ return ensure_constraints_pure( ++ default_constraints = default_constraints(repository_ctx), ++ target_constraints = repository_ctx.attr.target_constraints, ++ exec_constraints = repository_ctx.attr.exec_constraints, ++ ) +diff --git a/nixpkgs/toolchains/go.bzl b/nixpkgs/toolchains/go.bzl +index 0384a64..5b9f168 100644 +--- a/nixpkgs/toolchains/go.bzl ++++ b/nixpkgs/toolchains/go.bzl +@@ -8,7 +8,6 @@ dependencies on rules_go for those who don't need go toolchain. + """ + + load("//nixpkgs:nixpkgs.bzl", "nixpkgs_package") +-load("@bazel_tools//tools/cpp:lib_cc_configure.bzl", "get_cpu_value") + load("@io_bazel_rules_go//go/private:platforms.bzl", "PLATFORMS") + + def _detect_host_platform(ctx): +@@ -150,7 +149,6 @@ declare_toolchains("{goos}", "{goarch}") + """ + + def _nixpkgs_go_toolchain_impl(repository_ctx): +- cpu = get_cpu_value(repository_ctx) + goos, goarch = _detect_host_platform(repository_ctx) + content = go_toolchain_func.format( + sdk_repo = repository_ctx.attr.sdk_repo, diff --git a/compatibility/.bazelrc b/compatibility/.bazelrc index 52a39b859fdd..ad7b5b1e40ee 100644 --- a/compatibility/.bazelrc +++ b/compatibility/.bazelrc @@ -97,8 +97,8 @@ build:linux --action_env=LOCALE_ARCHIVE build:windows --action_env=JAVA_HOME # Tell bazel to use the nixpkgs Haskell toolchain on Linux and Darwin -build:linux --host_platform=@rules_haskell//haskell/platforms:linux_x86_64_nixpkgs -build:darwin --host_platform=@rules_haskell//haskell/platforms:darwin_x86_64_nixpkgs +build:linux --host_platform=@io_tweag_rules_nixpkgs//nixpkgs/platforms:host +build:darwin --host_platform=@io_tweag_rules_nixpkgs//nixpkgs/platforms:host # and GHC's gcc on Windows build:windows --crosstool_top=@rules_haskell_ghc_windows_amd64//:cc_toolchain diff --git a/compatibility/WORKSPACE b/compatibility/WORKSPACE index 961736a4bac4..88d31b2afac3 100644 --- a/compatibility/WORKSPACE +++ b/compatibility/WORKSPACE @@ -214,7 +214,7 @@ nixpkgs_package( nixpkgs_package( name = "node_nix", - attribute_path = "nodejsNested", + attribute_path = "nodejs14Nested", build_file_content = 'exports_files(glob(["node_nix/**"]))', fail_not_supported = False, nix_file = "@daml//nix:bazel.nix", diff --git a/daml-assistant/integration-tests/BUILD.bazel b/daml-assistant/integration-tests/BUILD.bazel index 6d11b215b82e..59f77cdd7efa 100644 --- a/daml-assistant/integration-tests/BUILD.bazel +++ b/daml-assistant/integration-tests/BUILD.bazel @@ -1,7 +1,7 @@ # Copyright (c) 2022 Digital Asset (Switzerland) GmbH and/or its affiliates. All rights reserved. # SPDX-License-Identifier: Apache-2.0 load("//bazel_tools:haskell.bzl", "da_haskell_library", "da_haskell_test") -load("@os_info//:os_info.bzl", "is_windows") +load("@os_info//:os_info.bzl", "is_darwin_arm64", "is_windows") load("@build_environment//:configuration.bzl", "mvn_version") genrule( @@ -199,7 +199,9 @@ genrule( "@nodejs//:node_bin", "@nodejs//:npm_bin", ], -) if not is_windows else None + # Broken on M1 due to puppeteer failing to install Chromium. + # See https://github.com/digital-asset/daml/pull/13250. +) if not (is_darwin_arm64 or is_windows) else None da_haskell_library( name = "create-daml-app-tests-lib", @@ -234,7 +236,7 @@ da_haskell_library( "//libs-haskell/da-hs-base", "//libs-haskell/test-utils", ], -) if not is_windows else None +) if not (is_darwin_arm64 or is_windows) else None da_haskell_test( name = "create-daml-app-tests-proj-name", @@ -255,7 +257,8 @@ da_haskell_test( flaky = True, # npm/jest/puppeteer can be flaky hackage_deps = ["base"], main_function = "DA.Daml.Assistant.CreateDamlAppTestsMain.main", - # Exclusive until we stop hardcoding port numbers in index.test.ts - tags = ["exclusive"], + tags = + # Exclusive until we stop hardcoding port numbers in index.test.ts + ["exclusive"], deps = [":create-daml-app-tests-lib"], -) if not is_windows else None +) if not (is_darwin_arm64 or is_windows) else None diff --git a/deps.bzl b/deps.bzl index f57039610dea..0fd53714eef4 100644 --- a/deps.bzl +++ b/deps.bzl @@ -45,9 +45,11 @@ rules_haskell_patches = [ "@com_github_digital_asset_daml//bazel_tools:haskell-rts-docs.patch", # This should be upstreamed "@com_github_digital_asset_daml//bazel_tools:haskell-ghc-includes.patch", + # This should be upstreamed + "@com_github_digital_asset_daml//bazel_tools:haskell-arm-m1.patch", ] -rules_nixpkgs_version = "81f61c4b5afcf50665b7073f7fce4c1755b4b9a3" -rules_nixpkgs_sha256 = "33fd540d0283cf9956d0a5a640acb1430c81539a84069114beaf9640c96d221a" +rules_nixpkgs_version = "b39b20edc4637032bc65f6a93af888463027767c" +rules_nixpkgs_sha256 = "69bbc7aceaeab20693ae8bdc46b7d7a208ef3d3f1e5c295bef474d9b2e6aa39f" rules_nixpkgs_patches = [ # On CI and locally we observe occasional segmantation faults # of nix. A known issue since Nix 2.2.2 is that HTTP2 support @@ -56,6 +58,8 @@ rules_nixpkgs_patches = [ # reportedly solves the issue. See # https://github.com/NixOS/nix/issues/2733#issuecomment-518324335 "@com_github_digital_asset_daml//bazel_tools:nixpkgs-disable-http2.patch", + # This should be upstreamed + "@com_github_digital_asset_daml//bazel_tools:rules-nixpkgs-arm.patch", ] buildifier_version = "4.0.0" @@ -88,6 +92,9 @@ daml_cheat_sheet_sha256 = "eb022565a929a69d869f0ab0497f02d1a3eacb4dafdafa076a82e platforms_version = "0.0.3" platforms_sha256 = "15b66b5219c03f9e8db34c1ac89c458bb94bfe055186e5505d5c6f09cb38307f" +rules_sh_version = "47b4d823128f484ec1b06aa20349c4898216f486" +rules_sh_sha256 = "107d4312073d80a9977d3ccff236060d3906bda939fa2fbda4d724268c5b5383" + def daml_deps(): if "platforms" not in native.existing_rules(): http_archive( @@ -97,6 +104,14 @@ def daml_deps(): urls = ["https://github.com/bazelbuild/platforms/archive/{version}.tar.gz".format(version = platforms_version)], ) + if "rules_sh" not in native.existing_rules(): + http_archive( + name = "rules_sh", + strip_prefix = "rules_sh-%s" % rules_sh_version, + urls = ["https://github.com/tweag/rules_sh/archive/%s.tar.gz" % rules_sh_version], + sha256 = rules_sh_sha256, + ) + if "rules_haskell" not in native.existing_rules(): http_archive( name = "rules_haskell", diff --git a/nix/bazel-go-toolchain.nix b/nix/bazel-go-toolchain.nix new file mode 100644 index 000000000000..afa0054abb80 --- /dev/null +++ b/nix/bazel-go-toolchain.nix @@ -0,0 +1 @@ +(import ./bazel.nix {}).go diff --git a/nix/bazel.nix b/nix/bazel.nix index 76625889c03f..348fd86a9e39 100644 --- a/nix/bazel.nix +++ b/nix/bazel.nix @@ -59,14 +59,53 @@ let shared = rec { # We need to have a file in GOPATH that we can use as # root_file in go_wrap_sdk. - go = pkgs.go.overrideAttrs (oldAttrs: { - doCheck = false; - postFixup = ''touch $out/share/go/ROOT''; - }); + go = pkgs.buildEnv { + name = "bazel-go-toolchain"; + paths = [ + pkgs.go + ]; + postBuild = '' + touch $out/ROOT + ln -s $out/share/go/{api,doc,lib,misc,pkg,src} $out/ + ''; + }; ghcPkgs = pkgs.haskell.packages.native-bignum.ghc902; - ghc = ghcPkgs.ghc; + ghc = + if system == "aarch64-darwin" then + pkgs.runCommand "ghc-aarch64-symlinks" { buildInputs = [ pkgs.makeWrapper ]; } '' + mkdir -p $out/bin + for tool in \ + ghc-9.0.2 \ + ghc-pkg \ + ghc-pkg-9.0.2 \ + ghci \ + ghci-9.0.2 \ + haddock \ + hp2ps \ + hpc \ + runghc-9.0.2 \ + runhaskell + do + ln -s ${ghcPkgs.ghc}/bin/$tool $out/bin/$tool + done; + mkdir -p $out/lib + ln -s ${ghcPkgs.ghc}/lib/ghc-9.0.2 $out/lib/ghc-9.0.2 + makeWrapper ${ghcPkgs.ghc}/bin/ghc $out/bin/ghc \ + --set CODESIGN_ALLOCATE ${pkgs.darwin.cctools}/bin/codesign_allocate \ + --prefix PATH : ${pkgs.llvmPackages_12.clang}/bin:${pkgs.llvmPackages_12.llvm}/bin + makeWrapper ${ghcPkgs.ghc}/bin/runghc $out/bin/runghc \ + --set CODESIGN_ALLOCATE ${pkgs.darwin.cctools}/bin/codesign_allocate \ + --prefix PATH : ${pkgs.llvmPackages_12.clang}/bin:${pkgs.llvmPackages_12.llvm}/bin + makeWrapper ${ghcPkgs.ghc}/bin/hsc2hs $out/bin/hsc2hs \ + --set CODESIGN_ALLOCATE ${pkgs.darwin.cctools}/bin/codesign_allocate \ + --prefix PATH : ${pkgs.llvmPackages_12.clang}/bin:${pkgs.llvmPackages_12.llvm}/bin + '' + else + ghcPkgs.ghc; + + # Deliberately not taken from ghcPkgs. This is a fully # static executable so it doesn’t pull in another GHC # and upstream nixpkgs does not cache packages for @@ -81,6 +120,7 @@ let shared = rec { # rules_nodejs expects nodejs in a subdirectory of a repository rule. # We use a linkFarm to fulfill this requirement. nodejsNested = pkgs.linkFarm "nodejs" [ { name = "node_nix"; path = pkgs.nodejs; }]; + nodejs14Nested = pkgs.linkFarm "nodejs" [ { name = "node_nix"; path = pkgs.nodejs14; }]; sass = pkgs.sass; @@ -145,7 +185,7 @@ let shared = rec { ; }; - bazel-cc-toolchain = pkgs.callPackage ./tools/bazel-cc-toolchain {}; + bazel-cc-toolchain = pkgs.callPackage ./tools/bazel-cc-toolchain { sigtool = pkgs.darwin.sigtool; }; }; in shared // (if pkgs.stdenv.isLinux then { inherit (pkgs) diff --git a/nix/nixpkgs.nix b/nix/nixpkgs.nix index 4cad1d11401e..9e86c744b136 100644 --- a/nix/nixpkgs.nix +++ b/nix/nixpkgs.nix @@ -10,7 +10,8 @@ let # package overrides overrides = _: pkgs: rec { - nodejs = pkgs.nodejs-14_x; + nodejs = pkgs.nodejs-16_x; + nodejs14 = pkgs.nodejs-14_x; ephemeralpg = pkgs.ephemeralpg.overrideAttrs(oldAttrs: { installPhase = '' mkdir -p $out @@ -37,6 +38,18 @@ let ./bazel-retry-cache.patch ]; }); + haskell = pkgs.haskell // { + compiler = pkgs.haskell.compiler // { + ghc902 = + if system == "aarch64-darwin" then + pkgs.haskell.compiler.ghc902.override(oldAttrs: { + buildTargetLlvmPackages = pkgs.llvmPackages_12; + llvmPackages = pkgs.llvmPackages_12; + }) + else + pkgs.haskell.compiler.ghc902; + }; + }; }; nixpkgs = import src { diff --git a/nix/system.nix b/nix/system.nix index 9b56795986c9..d81e537829d6 100644 --- a/nix/system.nix +++ b/nix/system.nix @@ -1 +1 @@ -if builtins.currentSystem == "aarch64-darwin" then "x86_64-darwin" else builtins.currentSystem +builtins.currentSystem diff --git a/nix/tools/bazel-cc-toolchain/default.nix b/nix/tools/bazel-cc-toolchain/default.nix index 0fa1d6ffb1f1..44fb6a9302c7 100644 --- a/nix/tools/bazel-cc-toolchain/default.nix +++ b/nix/tools/bazel-cc-toolchain/default.nix @@ -1,70 +1,60 @@ { stdenv , binutils +, bintools , buildEnv , darwin -, llvmPackages_7 +, llvmPackages_12 , makeWrapper +, wrapCCWith , overrideCC , runCommand +, writeTextFile +, sigtool }: # XXX On Darwin, workaround # https://github.com/NixOS/nixpkgs/issues/42059. See also # https://github.com/NixOS/nixpkgs/pull/41589. -let cc-darwin = - with darwin.apple_sdk.frameworks; - # Note (MK): For now we pin to clang 7 since newer versions fail to build abseil. - let stdenv = llvmPackages_7.stdenv; - in - runCommand "cc-wrapper-bazel" - { - buildInputs = [ stdenv.cc makeWrapper ]; - } - '' - mkdir -p $out/bin +let + postLinkSignHook = + writeTextFile { + name = "post-link-sign-hook"; + executable = true; - # Copy the content of pkgs.stdenv.cc - for i in ${stdenv.cc}/bin/* - do - ln -sf $i $out/bin - done + text = '' + CODESIGN_ALLOCATE=${darwin.cctools}/bin/codesign_allocate \ + ${sigtool}/bin/codesign -f -s - "$linkerOutput" + ''; + }; + darwinBinutils = darwin.binutils.override { inherit postLinkSignHook; }; + cc-darwin = + wrapCCWith rec { + cc = llvmPackages_12.clang; + bintools = darwinBinutils; + extraBuildCommands = with darwin.apple_sdk.frameworks; '' + echo "-Wno-unused-command-line-argument" >> $out/nix-support/cc-cflags + echo "-Wno-elaborated-enum-base" >> $out/nix-support/cc-cflags + echo "-mmacosx-version-min=${cc.darwinMinVersion}" >> $out/nix-support/cc-cflags + echo "-isystem ${llvmPackages_12.libcxx.dev}/include/c++/v1" >> $out/nix-support/cc-cflags + echo "-isystem ${llvmPackages_12.clang-unwrapped.lib}/lib/clang/${cc.version}/include" >> $out/nix-support/cc-cflags + echo "-F${CoreFoundation}/Library/Frameworks" >> $out/nix-support/cc-cflags + echo "-F${CoreServices}/Library/Frameworks" >> $out/nix-support/cc-cflags + echo "-F${Security}/Library/Frameworks" >> $out/nix-support/cc-cflags + echo "-F${Foundation}/Library/Frameworks" >> $out/nix-support/cc-cflags + echo "-L${llvmPackages_12.libcxx}/lib" >> $out/nix-support/cc-cflags + echo "-L${llvmPackages_12.libcxxabi}/lib" >> $out/nix-support/cc-cflags + echo "-L${darwin.libobjc}/lib" >> $out/nix-support/cc-cflags + ''; + }; - # Override cc - rm $out/bin/cc $out/bin/clang $out/bin/clang++ - - makeWrapper ${stdenv.cc}/bin/cc $out/bin/cc \ - --add-flags "-Wno-unused-command-line-argument \ - -mmacosx-version-min=10.14 \ - -isystem ${llvmPackages_7.libcxx}/include/c++/v1 \ - -F${CoreFoundation}/Library/Frameworks \ - -F${CoreServices}/Library/Frameworks \ - -F${Security}/Library/Frameworks \ - -F${Foundation}/Library/Frameworks \ - -L${llvmPackages_7.libcxx}/lib \ - -L${darwin.libobjc}/lib" - ''; - - cc-linux = runCommand "cc-wrapper-bazel" { - buildInputs = [ makeWrapper ]; - } - '' - mkdir -p $out/bin - - # Copy the content of pkgs.stdenv.cc - for i in ${stdenv.cc}/bin/* - do - ln -sf $i $out/bin - done - - # Override gcc - rm $out/bin/cc $out/bin/gcc $out/bin/g++ - - # We disable the fortify hardening as it causes issues with some - # packages built with bazel that set these flags themselves. - makeWrapper ${stdenv.cc}/bin/cc $out/bin/cc \ - --set hardeningDisable fortify - ''; + cc-linux = + wrapCCWith { + cc = stdenv.cc.overrideAttrs (oldAttrs: { + hardeningUnsupportedFlags = + ["fortify"] ++ oldAttrs.hardeningUnsupportedFlags or []; + }); + }; customStdenv = if stdenv.isDarwin @@ -73,5 +63,6 @@ let cc-darwin = in buildEnv { name = "bazel-cc-toolchain"; - paths = [ customStdenv.cc ] ++ (if stdenv.isDarwin then [ darwin.binutils ] else [ binutils ]); + paths = [ customStdenv.cc ] ++ (if stdenv.isDarwin then [ darwinBinutils ] else [ binutils ]); + ignoreCollisions = true; }