bazelbuild / platforms

Constraint values for specifying platforms and toolchains
Apache License 2.0
108 stars 73 forks source link

Issue specifying linux platform constraint to bazel workspace #80

Closed arpitpatel901 closed 11 months ago

arpitpatel901 commented 11 months ago

Currently I am attempting to build a starter grpc project with bazel[version 6.3.2]. Upon building, I get the following error, which I believe requires me to add the platform constraints[I believe the default platform is windows, while I am using an ubuntu os]:

ERROR: /home/arpit/.cache/bazel/_bazel_arpit/f196a2da91eeaec415a1459ab74281d6/external/bazel_tools/platforms/BUILD:89:6: in alias rule @bazel_tools//platforms:windows: Constraints from @bazel_tools//platforms have been removed. Please use constraints from @platforms repository embedded in Bazel, or preferably declare dependency on https://github.com/bazelbuild/platforms. See https://github.com/bazelbuild/bazel/issues/8622 for details.

Thus I added the following to my project

inside WORKSPACE

git_repository(
    name = "platforms",
    commit = "33a3b209f94856193266871b1545054afb90bb28",
    remote = "https://github.com/bazelbuild/platforms",
)

I then created a linux_platform.bzl file inside the workspace:

load("@bazel_tools//tools/build_defs:platforms/linux.bzl", "platform")

platform(
    name = "linux",
    constraint_values = {
        "@platforms//os:linux": 1,
    },
)

and then linked the linux_platform file inside my project BUILD file to force the build for linux os:

load("//:linux_platform.bzl", "linux")

However this fails, with the following error:

ERROR: Skipping ':all': while parsing ':all': error loading package 'projects/python_folder/grpc': at /home/arpit/bazel_ws/workspace-template/linux_platform.bzl:1:6: cannot load '@bazel_tools//tools/build_defs:platforms/linux.bzl': no such file

I am new to bazel environment [previously have been using catkin/cmake], so I apologize for the noob question. What am I missing here, I see that linux.bzl is not part of the platforms repo, but I thought should it would built with the os folder. How do I provide the linux constraint ?

aiuto commented 11 months ago

A few things.

arpitpatel901 commented 11 months ago

Thanks @aiuto for the quick response ! :)

That is weird since I am building for ubuntu and running it on ubuntu too :/ I changed the import to use http

http_archive(
    name = "platforms",
    urls = [
        "https://mirror.bazel.build/github.com/bazelbuild/platforms/releases/download/0.0.8/platforms-0.0.8.tar.gz",
        "https://github.com/bazelbuild/platforms/releases/download/0.0.8/platforms-0.0.8.tar.gz",
    ],
    sha256 = "8150406605389ececb6da07cbcb509d5637a3ab9a24bc69b1101531367d89d74",
)

Removed links to linux.bz[load] in my project build file and directly used the native platform format:

platform(
    name = "linux_x86",
    constraint_values = [
        "@platforms//os:linux",
        "@platforms//cpu:x86_64",
        ":glibc_2_25",
    ],
)

But still for some reason, during build it is trying to build for windows[did a bazel clean --expunge in the middle]:

arpit@arpit-ThinkPad-T460s:~/bazel_ws/workspace-template/projects/python_folder/grpc$ bazel build :all
Starting local Bazel server and connecting to it...
DEBUG: Rule 'com_google_absl' indicated that a canonical reproducible form can be obtained by modifying arguments commit = "215105818dfde3174fe799600bb0f3cae233d0bf" and dropping ["tag"]
DEBUG: Repository com_google_absl instantiated at:
  /home/arpit/bazel_ws/workspace-template/WORKSPACE:48:15: in <toplevel>
Repository rule git_repository defined at:
  /home/arpit/.cache/bazel/_bazel_arpit/f196a2da91eeaec415a1459ab74281d6/external/bazel_tools/tools/build_defs/repo/git.bzl:181:33: in <toplevel>
ERROR: /home/arpit/.cache/bazel/_bazel_arpit/f196a2da91eeaec415a1459ab74281d6/external/bazel_tools/platforms/BUILD:89:6: in alias rule @bazel_tools//platforms:windows: Constraints from @bazel_tools//platforms have been removed. Please use constraints from @platforms repository embedded in Bazel, or preferably declare dependency on https://github.com/bazelbuild/platforms. See https://github.com/bazelbuild/bazel/issues/8622 for details.
ERROR: /home/arpit/.cache/bazel/_bazel_arpit/f196a2da91eeaec415a1459ab74281d6/external/bazel_tools/platforms/BUILD:89:6: Analysis of target '@bazel_tools//platforms:windows' failed
ERROR: /home/arpit/.cache/bazel/_bazel_arpit/f196a2da91eeaec415a1459ab74281d6/external/upb/BUILD:83:11: errors encountered resolving select() keys for @upb//:upb
ERROR: Analysis of target '//projects/python_folder/grpc:helloworld_py_grpc' failed; build aborted: 
INFO: Elapsed time: 18.628s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (82 packages loaded, 1327 targets configured)
    Fetching repository @python_interpreter; starting
    Fetching repository @boringssl; starting
    Fetching ...81d6/external/boringssl; Extracting b9232f9e27e5668bc0414879dcdedb2a59ea75f2.tar.gz
    Fetching ...f196a2da91eeaec415a1459ab74281d6/external/python_interpreter; Extracting python.tar
arpit@arpit-ThinkPad-T460s:~/bazel_ws/workspace-template/projects/python_folder/grpc$ 

This is how I am loading the grpc library

Inside project WORKSPACE file:

http_archive(
    name = "com_github_grpc_grpc",
    strip_prefix = "grpc-1.45.0",
    sha256 = "ec19657a677d49af59aa806ec299c070c882986c9fcc022b1c22c2a3caf01bcd",
    urls = ["https://github.com/grpc/grpc/archive/refs/tags/v1.45.0.tar.gz"],
)
load("@com_github_grpc_grpc//bazel:grpc_deps.bzl", "grpc_deps")
grpc_deps()
load("@com_github_grpc_grpc//bazel:grpc_extra_deps.bzl", "grpc_extra_deps")
grpc_extra_deps()

and then inside the project BUILD:

load("@rules_proto//proto:defs.bzl", "proto_library")
load("@com_github_grpc_grpc//bazel:python_rules.bzl", "py_grpc_library", "py_proto_library")

Is there something wrong/outdated with my grpc imports ?

arpitpatel901 commented 11 months ago

I was able to solve the platform windows issue by following instructions from https://rules-proto-grpc.com/en/latest/lang/python.html#python-proto-compile for writing a grpc protobuf. This particular issue is solved. Thank you !