Skip to content

Commit

Permalink
feat(bazel): support building C++ libs on windows platform (#1873)
Browse files Browse the repository at this point in the history
<!--
**Thanks for contributing to Fury.**

**If this is your first time opening a PR on fury, you can refer to
[CONTRIBUTING.md](https://github.com/apache/fury/blob/main/CONTRIBUTING.md).**

Contribution Checklist

- The **Apache Fury (incubating)** community has restrictions on the
naming of pr titles. You can also find instructions in
[CONTRIBUTING.md](https://github.com/apache/fury/blob/main/CONTRIBUTING.md).

- Fury has a strong focus on performance. If the PR you submit will have
an impact on performance, please benchmark it first and provide the
benchmark result here.
-->

## What does this PR do?

Make bazel happy on `Windows OS`.

1. Rewrite a little codes in `row.cc`, then it could be compiled by MSVC
([VLA is not
supported](https://devblogs.microsoft.com/cppblog/c11-and-c17-standard-support-arriving-in-msvc/))
2. Add MSVC flag
[`/Zc:preprocessor`](https://learn.microsoft.com/en-us/cpp/build/reference/zc-preprocessor?view=msvc-170)
to enable C99/C11
3. Add MSVC flag
[`/utf-8`](https://learn.microsoft.com/en-us/cpp/build/reference/utf-8-set-source-and-executable-character-sets-to-utf-8?view=msvc-170)
to set source and execution character sets to UTF-8.
4. Load arrow(arrow_python) interface libraries `*.lib` to the linker
5. Add a new CI env `windows-2022` to build fury cpp libraries

<!-- Describe the purpose of this PR. -->

## Related issues

#798 

<!--
Is there any related issue? Please attach here.

- #xxxx0
- #xxxx1
- #xxxx2
-->

## Does this PR introduce any user-facing change?

<!--
If any user-facing interface changes, please [open an
issue](https://github.com/apache/fury/issues/new/choose) describing the
need to do so and update the document if necessary.
-->

- [ ] Does this PR introduce any public API change?
- [ ] Does this PR introduce any binary protocol compatibility change?

## Benchmark

<!--
When the PR has an impact on performance (if you don't know whether the
PR will have an impact on performance, you can submit the PR first, and
if it will have impact on performance, the code reviewer will explain
it), be sure to attach a benchmark data here.
-->

---------

Signed-off-by: Junduo Dong <andj4cn@gmail.com>
  • Loading branch information
An-DJ authored Oct 9, 2024
1 parent c8b63f4 commit 0e20458
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 20 deletions.
4 changes: 3 additions & 1 deletion .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ build:linux --force_pic
build:macos --force_pic
build:clang-cl --compiler=clang-cl
build:msvc --compiler=msvc-cl
build:windows --compiler=msvc-cl

# test config
test --build_tests_only
Expand All @@ -38,4 +39,5 @@ test --test_output=all
build:linux --cxxopt="-std=c++17" --linkopt="-pthread"
build:macos --cxxopt="-std=c++17" --linkopt="-pthread"
build:clang-cl --cxxopt="-std=c++17"
build:windows --cxxopt="/std:c++17"
build:windows --cxxopt="/std:c++17" --cxxopt="/Zc:preprocessor" --cxxopt="/utf-8"
build:msvc --cxxopt="/std:c++17" --cxxopt="/Zc:preprocessor" --cxxopt="/utf-8"
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ jobs:
name: C++ CI
strategy:
matrix:
os: [ubuntu-latest, macos-12, macos-14] # macos-12: x86, macos-14: arm64
os: [ubuntu-latest, macos-12, macos-14, windows-2022] # macos-12: x86, macos-14: arm64

Check warning on line 184 in .github/workflows/ci.yml

View workflow job for this annotation

GitHub Actions / 🍏 YAML

184:63 [comments] too few spaces before comment
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
Expand Down
62 changes: 62 additions & 0 deletions bazel/arrow/BUILD.windows.bzl
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

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

load("@rules_cc//cc:defs.bzl", "cc_library", "cc_import")

cc_library(
name = "arrow",
hdrs = [":arrow_header_include"],
includes = ["include"],
deps = [":arrow_shared_library"],
visibility = ["//visibility:public"],
)

cc_import(
name = "arrow_shared_library",
interface_library = ":libarrow_interface",
shared_library = ":libarrow",
visibility = ["//visibility:public"],
)

cc_import(
name = "arrow_python_shared_library",
interface_library = ":libarrow_python_interface",
shared_library = ":libarrow_python",
visibility = ["//visibility:public"],
)

cc_library(
name = "arrow_header_lib",
hdrs = [":arrow_header_include"],
includes = ["include"],
visibility = ["//visibility:public"],
)

cc_library(
name="python_numpy_headers",
hdrs=[":python_numpy_include"],
includes=["python_numpy_include"],
)

%{ARROW_HEADER_GENRULE}
%{ARROW_LIBRARY_GENRULE}
%{ARROW_ITF_LIBRARY_GENRULE}
%{ARROW_PYTHON_LIBRARY_GENRULE}
%{ARROW_PYTHON_ITF_LIB_GENRULE}
%{PYTHON_NUMPY_INCLUDE_GENRULE}
58 changes: 43 additions & 15 deletions bazel/arrow/pyarrow_configure.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,12 @@ def _get_pyarrow_include(repository_ctx, python_bin="python3"):
error_details=(
"Is the Python binary path set up right? " + "(See ./configure or "
+ python_bin + ".) " + "Is distutils installed?"))
return result.stdout.splitlines()[0]
return result.stdout.splitlines()[0].replace('\\', '/')

def _get_pyarrow_shared_library(repository_ctx, library_name, python_bin="python3"):
"""Gets the pyarrow shared library path."""
code = """import pyarrow, os, glob;print(glob.glob(os.path.join(""" +\
"""os.path.dirname(pyarrow.__file__), 'lib{}.*'))[0])""".format(library_name)
"""os.path.dirname(pyarrow.__file__), '{}'))[0])""".format(library_name)
result = _execute(
repository_ctx, [
python_bin, "-c", code
Expand All @@ -186,7 +186,7 @@ def _get_pyarrow_shared_library(repository_ctx, library_name, python_bin="python
error_details=(
"Is the Python binary path set up right? " + "(See ./configure or "
+ python_bin + ".) " + "Is distutils installed?"))
return result.stdout.splitlines()[0]
return result.stdout.splitlines()[0].replace('\\', '/')

#python numpy include
def _get_python_numpy_include(repository_ctx, python_bin="python3"):
Expand All @@ -199,39 +199,67 @@ def _get_python_numpy_include(repository_ctx, python_bin="python3"):
error_details=(
"Is the Python binary path set up right? " + "(See ./configure or "
+ python_bin + ".) " + "Is distutils installed?"))
return result.stdout.splitlines()[0]
return result.stdout.splitlines()[0].replace('\\', '/')

def _pyarrow_pip_impl(repository_ctx):
arrow_header_dir = _get_pyarrow_include(repository_ctx)
python_bin = "python3"

# python 3.x is usually named as `python` by default on windows.
if _is_windows(repository_ctx):
python_bin = "python"

arrow_header_dir = _get_pyarrow_include(repository_ctx, python_bin)
arrow_header_rule = _symlink_genrule_for_dir(
repository_ctx,
arrow_header_dir,
"include",
"arrow_header_include",
)

arrow_library_path = _get_pyarrow_shared_library(repository_ctx, "arrow")
arrow_library_path = _get_pyarrow_shared_library(repository_ctx, "arrow.dll" if _is_windows(repository_ctx) else "libarrow.*", python_bin)
arrow_library = arrow_library_path.rsplit("/",1 )[-1]
arrow_library_rule = _symlink_genrule_for_dir(
repository_ctx, None, "", "libarrow", [arrow_library_path], [arrow_library])

arrow_python_library_path = _get_pyarrow_shared_library(repository_ctx, "arrow_python")
arrow_python_library_path = _get_pyarrow_shared_library(repository_ctx, "arrow_python.dll" if _is_windows(repository_ctx) else "libarrow_python.*", python_bin)
arrow_python_library = arrow_python_library_path.rsplit("/",1 )[-1]
arrow_python_library_rule = _symlink_genrule_for_dir(
repository_ctx, None, "", "libarrow_python",
[arrow_python_library_path], [arrow_python_library])

python_numpy_include = _get_python_numpy_include(repository_ctx)
python_numpy_include = _get_python_numpy_include(repository_ctx, python_bin)
python_numpy_include_rule = _symlink_genrule_for_dir(
repository_ctx, python_numpy_include, 'python_numpy_include', 'python_numpy_include')

build_tpl = repository_ctx.path(Label("//bazel/arrow:BUILD.tpl.bzl"))
repository_ctx.template("BUILD", build_tpl, {
"%{ARROW_HEADER_GENRULE}": arrow_header_rule,
"%{ARROW_LIBRARY_GENRULE}": arrow_library_rule,
"%{ARROW_PYTHON_LIBRARY_GENRULE}": arrow_python_library_rule,
"%{PYTHON_NUMPY_INCLUDE_GENRULE}": python_numpy_include_rule,
})
if _is_windows(repository_ctx):
arrow_interface_library_path = _get_pyarrow_shared_library(repository_ctx, "arrow.lib", python_bin)
arrow_interface_library = arrow_interface_library_path.rsplit("/",1 )[-1]
arrow_interface_library_rule = _symlink_genrule_for_dir(
repository_ctx, None, "", "libarrow_interface", [arrow_interface_library_path], [arrow_interface_library])

arrow_python_interface_library_path = _get_pyarrow_shared_library(repository_ctx, "arrow_python.lib", python_bin)
arrow_python_interface_library = arrow_python_interface_library_path.rsplit("/",1 )[-1]
arrow_python_interface_library_rule = _symlink_genrule_for_dir(
repository_ctx, None, "", "libarrow_python_interface",
[arrow_python_interface_library_path], [arrow_python_interface_library])

build_tpl = repository_ctx.path(Label("//bazel/arrow:BUILD.windows.bzl"))
repository_ctx.template("BUILD", build_tpl, {
"%{ARROW_HEADER_GENRULE}": arrow_header_rule,
"%{ARROW_LIBRARY_GENRULE}": arrow_library_rule,
"%{ARROW_ITF_LIBRARY_GENRULE}": arrow_interface_library_rule,
"%{ARROW_PYTHON_LIBRARY_GENRULE}": arrow_python_library_rule,
"%{ARROW_PYTHON_ITF_LIB_GENRULE}": arrow_python_interface_library_rule,
"%{PYTHON_NUMPY_INCLUDE_GENRULE}": python_numpy_include_rule,
})
else:
build_tpl = repository_ctx.path(Label("//bazel/arrow:BUILD.tpl.bzl"))
repository_ctx.template("BUILD", build_tpl, {
"%{ARROW_HEADER_GENRULE}": arrow_header_rule,
"%{ARROW_LIBRARY_GENRULE}": arrow_library_rule,
"%{ARROW_PYTHON_LIBRARY_GENRULE}": arrow_python_library_rule,
"%{PYTHON_NUMPY_INCLUDE_GENRULE}": python_numpy_include_rule,
})

pyarrow_configure = repository_rule(
implementation = _pyarrow_pip_impl,
Expand Down
4 changes: 3 additions & 1 deletion ci/run_ci.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@

PYARROW_VERSION = "14.0.0"

NUMPY_VERSION = "1.26.4"

PROJECT_ROOT_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "../")

logging.basicConfig(
Expand Down Expand Up @@ -115,7 +117,7 @@ def _run_js():
def _install_cpp_deps():
_exec_cmd(f"pip install pyarrow=={PYARROW_VERSION}")
_exec_cmd("pip install psutil")
_exec_cmd("pip install 'numpy<2.0.0'")
_exec_cmd(f"pip install numpy=={NUMPY_VERSION}")
_install_bazel()


Expand Down
4 changes: 2 additions & 2 deletions cpp/fury/row/row.cc
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,8 @@ int *ArrayData::GetDimensions(ArrayData &array, int num_dims) {
// use deep-first search to search to numDimensions-1 layer to get dimensions.
int depth = 0;
auto dimensions = new int[num_dims];
int start_from_lefts[num_dims];
ArrayData *arrs[num_dims]; // root to current node
std::vector<int> start_from_lefts(num_dims);
std::vector<const ArrayData *> arrs(num_dims); // root to current node
ArrayData &arr = array;
while (depth < num_dims) {
arrs[depth] = &arr;
Expand Down

0 comments on commit 0e20458

Please sign in to comment.