docs: add dependencies example
diff --git a/examples/MODULE.bazel b/examples/MODULE.bazel index f54d398..85f562d 100644 --- a/examples/MODULE.bazel +++ b/examples/MODULE.bazel
@@ -6,6 +6,7 @@ path = "../", ) +bazel_dep(name = "rules_cc", version = "0.1.1") bazel_dep(name = "aspect_bazel_lib", version = "2.10.0") bazel_dep(name = "bazel_skylib", version = "1.7.1")
diff --git a/examples/dependencies/BUILD.bazel b/examples/dependencies/BUILD.bazel new file mode 100644 index 0000000..b3ac9e8 --- /dev/null +++ b/examples/dependencies/BUILD.bazel
@@ -0,0 +1,36 @@ +load("@doxygen//:doxygen.bzl", "doxygen") +load("@rules_cc//cc:defs.bzl", "cc_library") + +cc_library( + name = "add", + srcs = ["add.cpp"], + hdrs = ["add.h"], +) + +cc_library( + name = "sub", + srcs = ["sub.cpp"], + hdrs = ["sub.h"], +) + +cc_library( + name = "lib", + hdrs = ["lib.h"], + deps = [ + ":add", + ":sub", + ], +) + +cc_library( + name = "main", + srcs = ["main.cpp"], + deps = [":lib"], +) + +doxygen( + name = "doxygen", + project_brief = "Example project for doxygen", + project_name = "dependencies", + deps = [":main"], +)
diff --git a/examples/dependencies/README.md b/examples/dependencies/README.md new file mode 100644 index 0000000..41bdeae --- /dev/null +++ b/examples/dependencies/README.md
@@ -0,0 +1,66 @@ +# Dependencies example + +In this example, instead of specifying the files to be included in the Doxygen documentation using the `srcs` attribute with a glob, we use the `deps` attribute. +This allows us to select a target and immediately include all of the files in its `srcs`, `hdrs`, and `data` attributes, along with all of its transitive dependencies. + +```bash +bazel build //dependencies:doxygen +``` + +## Showcase + +In a very common scenario, imagine working on a C++ project with the following build file: + +```bazel +# BUILD.bazel +load("@rules_cc//cc:defs.bzl", "cc_library") + +cc_library( + name = "add", + srcs = ["add.cpp"], + hdrs = ["add.h"], +) + +cc_library( + name = "sub", + srcs = ["sub.cpp"], + hdrs = ["sub.h"], +) + +cc_library( + name = "lib", + hdrs = ["lib.h"], + deps = [ + ":add", + ":sub", + ], +) + +cc_library( + name = "main", + srcs = ["main.cpp"], + deps = [":lib"], +) +``` + +The graph of dependencies looks like this: + +```mermaid +graph TD; + A[main.cpp] --> B[lib.h]; + B --> C[add.h<br>add.cpp]; + B --> D[sub.h<br>sub.cpp]; +``` + +If we want to include all of the files in the documentation, we can use the `deps` attribute to collect the `main` target's files and those of its transitive dependencies, like this: + +```bazel +# BUILD.bazel +load("@doxygen//:doxygen.bzl", "doxygen") + +doxygen( + name = "doxygen", + deps = [":main"], + doxygen_config = ":Doxyfile", +) +```
diff --git a/examples/dependencies/add.cpp b/examples/dependencies/add.cpp new file mode 100644 index 0000000..47419aa --- /dev/null +++ b/examples/dependencies/add.cpp
@@ -0,0 +1,13 @@ +/** + * @file add.cpp + * @author Ernesto Casablanca (casablancaernesto@gmail.com) + * @copyright 2024 + */ + +#include "add.h" + +namespace lib { + +int add(int a, int b) { return a + b; } + +} \ No newline at end of file
diff --git a/examples/dependencies/add.h b/examples/dependencies/add.h new file mode 100644 index 0000000..39de229 --- /dev/null +++ b/examples/dependencies/add.h
@@ -0,0 +1,22 @@ +/** + * @file add.h + * @author Ernesto Casablanca (casablancaernesto@gmail.com) + * @copyright 2024 + */ +#pragma once + +namespace lib { + +/** + * @brief Add two integers + * + * Who knows what the result will be? + * @note This function is very complex. Use it with caution. + * @warning The result can be greater than the maximum value that can be stored! + * @param a First integer + * @param b Second integer + * @return Sum of a and b + */ +int add(int a, int b); + +} // namespace lib \ No newline at end of file
diff --git a/examples/dependencies/lib.h b/examples/dependencies/lib.h new file mode 100644 index 0000000..2232808 --- /dev/null +++ b/examples/dependencies/lib.h
@@ -0,0 +1,16 @@ +/** + * @file lib.h + * @author Ernesto Casablanca (casablancaernesto@gmail.com) + * @copyright 2024 + */ +#pragma once + +#include "add.h" +#include "sub.h" + +/** + * @namespace lib + * An amazing library providing a lot of functions to do math operations. + * @note This library is very complex. Use it with caution. + */ +namespace lib {} // namespace lib \ No newline at end of file
diff --git a/examples/dependencies/main.cpp b/examples/dependencies/main.cpp new file mode 100644 index 0000000..b2bdaa5 --- /dev/null +++ b/examples/dependencies/main.cpp
@@ -0,0 +1,17 @@ +/** + * @file lib.cpp + * @author Ernesto Casablanca (casablancaernesto@gmail.com) + * @copyright 2024 + */ + +#include <iostream> + +#include "lib.h" + +int main(int, char*[]) { + int a = 5; + int b = 10; + std::cout << "a + b: " << lib::add(a, b) << std::endl; + std::cout << "a - b: " << lib::sub(a, b) << std::endl; + return 0; +}
diff --git a/examples/dependencies/sub.cpp b/examples/dependencies/sub.cpp new file mode 100644 index 0000000..1819360 --- /dev/null +++ b/examples/dependencies/sub.cpp
@@ -0,0 +1,13 @@ +/** + * @file sub.cpp + * @author Ernesto Casablanca (casablancaernesto@gmail.com) + * @copyright 2024 + */ + +#include "sub.h" + +namespace lib { + +int sub(int a, int b) { return a - b; } + +} // namespace lib \ No newline at end of file
diff --git a/examples/dependencies/sub.h b/examples/dependencies/sub.h new file mode 100644 index 0000000..d50df5c --- /dev/null +++ b/examples/dependencies/sub.h
@@ -0,0 +1,22 @@ +/** + * @file sub.h + * @author Ernesto Casablanca (casablancaernesto@gmail.com) + * @copyright 2024 + */ +#pragma once + +namespace lib { + +/** + * @brief Substract two integers + * + * Who knows what the result will be? + * @note This function is very complex. Use it with caution. + * @warning The result can be greater than the maximum value that can be stored! + * @param a First integer + * @param b Second integer + * @return Subtraction of a and b + */ +int sub(int a, int b); + +} // namespace lib \ No newline at end of file