Converting opencv from cmake to bazel

532 views
Skip to first unread message

harika sanga

unread,
Jun 17, 2021, 12:45:12 PM6/17/21
to bazel-discuss
Hi All,

I'm trying to convert opencv from cmake to bazel using this(git clone https://github.com/opencv/opencv.git)
under opencv/3rdparty/openjpeg/ converting openjp2 to bazel

In the process when i'm trying to build using belong content in build file:
cc_library(
    name = ""libopenjp2,
    srcs = ["*.c"],
    hdrs = glob(["*.h"],
        ),
)

Getting below error:
Use --sandbox_debug to see verbose messages from the sandbox gcc failed: error executing command /usr/bin/gcc -U_FORTIFY_SOURCE -fstack-protector -Wall -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer -MD -MF bazel-out/k8-fastbuild/bin/openjp2/_objs/cio/cio.pic.d ... (remaining 15 argument(s) skipped)

Use --sandbox_debug to see verbose messages from the sandbox
In file included from openjp2/cio.c:40:
openjp2/opj_includes.h:40:10: fatal error: opj_config_private.h: No such file or directory
   40 | #include "opj_config_private.h"

      |          ^~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
Target //openjp2:cio failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 0.288s, Critical Path: 0.05s
INFO: 2 processes: 2 internal.
FAILED: Build did NOT complete successfully


So tried creating the missing header file by creating a rule to create file(generate_file.bzl):
# -*- python -*-

def _generate_file_impl(ctx):
    out = ctx.actions.declare_file(ctx.label.name)
    ctx.actions.write(out, ctx.attr.content, ctx.attr.is_executable)
    return [DefaultInfo(
        files = depset([out]),
        data_runfiles = ctx.runfiles(files = [out]),
    )]

generate_file = rule(
    attrs = {
        "content": attr.string(mandatory = True),
        "is_executable": attr.bool(default = False),
    },
    output_to_genfiles = True,
    implementation = _generate_file_impl,
)

"""Generate a file with specified content.

This creates a rule to generate a file with specified content (which is either
static or has been previously computed).

Args:
    content (:obj:`str`): Desired content of the generated file.
"""
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#
using the rule file is creating in bazel-bin location, but i want to create it in source code location same as cmake

File location in cmake after build:
opencv/build/3rdparty/openjpeg/openjp2/opj_config_private.h

File location in bazel after build
opencv/3rdparty/openjpeg/openjp2/bazel-bin$ ls
liblibopenjp2.a-2.params  _objs  opj_config_private.h

How to generate file in source location same as how cmake does

Austin Schuh

unread,
Jun 17, 2021, 12:55:55 PM6/17/21
to harika sanga, bazel-discuss
Pass in an include path so your cc_library can use it without moving the paths around.  You can always put a header in a sub folder and include the folder if you need a more interesting path.

Heads up, building opencv is a bit rough from source...  You'll need to reproduce a lot of the logic in the cmake files to handle architecture specific assembly code implementations for different platforms.

The way I've had the most success porting a compile over is to do a cmake build at full verbosity so I get all the compiler command lines, and then slowly work on figuring out how to recreate each of those in Bazel.  Some are easy (all files in a directory get built with the default compiler flags), and some are harder.

Austin

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/43a42546-42c7-4bd6-9676-c9dcee037b95n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages