From 7fcea0d6749fc1da518f91511eabefb5e4438d96 Mon Sep 17 00:00:00 2001 From: MiniPiku <21parthib2006@gmail.com> Date: Thu, 13 Mar 2025 02:52:37 +0530 Subject: [PATCH 1/4] fix: include header files in CBuilder sources for cache invalidation - Added .h files to sources list in example/build/native_add_library/hook/build.dart - Documented sources parameter in CBuilder.library constructor to include .h files - Added note to native_assets_cli/README.md about including .h files in sources --- .../test_data/native_subtract/hook/build.dart | 5 ++++- pkgs/native_assets_cli/README.md | 17 +++++++++++++++++ .../example/build/use_dart_api/hook/build.dart | 7 ++++++- pkgs/native_assets_cli/lib/src/api/build.dart | 1 + .../lib/src/cbuilder/cbuilder.dart | 18 ++++++++++++++++++ 5 files changed, 46 insertions(+), 2 deletions(-) diff --git a/pkgs/native_assets_builder/test_data/native_subtract/hook/build.dart b/pkgs/native_assets_builder/test_data/native_subtract/hook/build.dart index 907a0fcd82..008478078c 100644 --- a/pkgs/native_assets_builder/test_data/native_subtract/hook/build.dart +++ b/pkgs/native_assets_builder/test_data/native_subtract/hook/build.dart @@ -12,7 +12,10 @@ void main(List arguments) async { final cbuilder = CBuilder.library( name: packageName, assetName: 'src/${packageName}_bindings_generated.dart', - sources: ['src/$packageName.c'], + sources: [ + 'src/$packageName.c', + 'src/$packageName.h', + ], ); await cbuilder.run( input: input, diff --git a/pkgs/native_assets_cli/README.md b/pkgs/native_assets_cli/README.md index 687f038cd6..57a0900a69 100644 --- a/pkgs/native_assets_cli/README.md +++ b/pkgs/native_assets_cli/README.md @@ -50,6 +50,23 @@ experiment is only available on the master channel. We do breaking changes regularly! So frequently bump `package:native_assets_cli` and use dev/master SDK for CI. +**Note:** When configuring `CBuilder` in your `hook/build.dart`, +include both `.c` source files and `.h` header files in the `sources` list. +This ensures that changes to header files (e.g., `native_add_library.h`) +invalidate the build cache, triggering a rebuild when necessary. For example: +```dart +final cbuilder = CBuilder.library( + name: 'native_add_library', + assetName: 'src/native_add_library_bindings_generated.dart', + sources: [ + 'src/native_add_library.c', + 'src/native_add_library.h', + 'src/dart_api_dl.c', + 'src/dart_api_dl.h', + ], +); +``` + ## Development The development of the feature can be tracked in [dart-lang#50565], diff --git a/pkgs/native_assets_cli/example/build/use_dart_api/hook/build.dart b/pkgs/native_assets_cli/example/build/use_dart_api/hook/build.dart index bebee31b2a..0833494d66 100644 --- a/pkgs/native_assets_cli/example/build/use_dart_api/hook/build.dart +++ b/pkgs/native_assets_cli/example/build/use_dart_api/hook/build.dart @@ -12,7 +12,12 @@ void main(List arguments) async { final cbuilder = CBuilder.library( name: packageName, assetName: 'src/${packageName}_bindings_generated.dart', - sources: ['src/$packageName.c', 'src/dart_api_dl.c'], + sources: [ + 'src/$packageName.c', + 'src/$packageName.h', + 'src/dart_api_dl.c', + 'src/dart_api_dl.h', + ], ); await cbuilder.run( input: input, diff --git a/pkgs/native_assets_cli/lib/src/api/build.dart b/pkgs/native_assets_cli/lib/src/api/build.dart index 25ec1bb6d0..436b4d06bc 100644 --- a/pkgs/native_assets_cli/lib/src/api/build.dart +++ b/pkgs/native_assets_cli/lib/src/api/build.dart @@ -31,6 +31,7 @@ import '../validation.dart'; /// assetName: '$packageName.dart', /// sources: [ /// 'src/$packageName.c', +/// 'src/$packageName.h', /// ], /// ); /// await cbuilder.run( diff --git a/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart b/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart index b2b40c8ad2..0c3aab8e37 100644 --- a/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart +++ b/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart @@ -53,6 +53,24 @@ class CBuilder extends CTool implements Builder { CBuilder.library({ required super.name, super.assetName, + /// The list of source files to build the library. + /// + /// This should include both C source files (e.g., `.c`) and header files + /// (e.g., `.h`). Including header files ensures that changes to them + /// invalidate the build cache, triggering recompilation when necessary. + /// For example, for a package named `native_add_library`: + /// ```dart + /// sources: [ + /// 'src/native_add_library.c', + /// 'src/native_add_library.h', + /// 'src/dart_api_dl.c', + /// 'src/dart_api_dl.h', + /// ], + /// ``` + /// Supported by Clang-like compilers, which can optimize with precompiled + /// headers when `.h` files are included. If a compiler does not support this, + /// the build system may filter `.h` files from the compilation step while + /// still tracking them as dependencies. super.sources = const [], super.includes = const [], super.frameworks = CTool.defaultFrameworks, From 2ff4ffa41e4b3f8275bf6b41552d4bd5aaa114bb Mon Sep 17 00:00:00 2001 From: MiniPiku <21parthib2006@gmail.com> Date: Thu, 13 Mar 2025 17:08:01 +0530 Subject: [PATCH 2/4] shifted the comments for better understanding --- pkgs/native_assets_cli/README.md | 19 +------------- .../lib/src/cbuilder/cbuilder.dart | 25 ++++++++++++++----- .../lib/src/cbuilder/ctool.dart | 7 ++++++ 3 files changed, 27 insertions(+), 24 deletions(-) diff --git a/pkgs/native_assets_cli/README.md b/pkgs/native_assets_cli/README.md index 57a0900a69..b7ab5c9921 100644 --- a/pkgs/native_assets_cli/README.md +++ b/pkgs/native_assets_cli/README.md @@ -43,30 +43,13 @@ assets work on Dart stable, but prefer using the dev releases as we regularly break things. To use native assets in Flutter, use -`flutter config --enable-experiment=native-assets` and then +`flutter config --enable-experiment=native-assets` and t `flutter create --template=package_ffi `. In Flutter, the experiment is only available on the master channel. We do breaking changes regularly! So frequently bump `package:native_assets_cli` and use dev/master SDK for CI. -**Note:** When configuring `CBuilder` in your `hook/build.dart`, -include both `.c` source files and `.h` header files in the `sources` list. -This ensures that changes to header files (e.g., `native_add_library.h`) -invalidate the build cache, triggering a rebuild when necessary. For example: -```dart -final cbuilder = CBuilder.library( - name: 'native_add_library', - assetName: 'src/native_add_library_bindings_generated.dart', - sources: [ - 'src/native_add_library.c', - 'src/native_add_library.h', - 'src/dart_api_dl.c', - 'src/dart_api_dl.h', - ], -); -``` - ## Development The development of the feature can be tracked in [dart-lang#50565], diff --git a/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart b/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart index 0c3aab8e37..8bb3083a28 100644 --- a/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart +++ b/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart @@ -17,6 +17,25 @@ import 'output_type.dart'; import 'run_cbuilder.dart'; /// Specification for building an artifact with a C compiler. +/// +/// **Note:** When configuring `CBuilder` in your `hook/build.dart`, +/// include both `.c` source files and `.h` header files in the `sources` list. +/// This ensures that changes to header files (e.g., `native_add_library.h`) +/// invalidate the build cache, triggering a rebuild when necessary. For example: +/// +/// ```dart +/// final cbuilder = CBuilder.library( +/// name: 'native_add_library', +/// assetName: 'src/native_add_library_bindings_generated.dart', +/// sources: [ +/// 'src/native_add_library.c', +/// 'src/native_add_library.h', +/// ], +/// ); +/// ``` +/// +/// This class provides options to define sources, dependencies, +/// and compiler configurations for building C-based native libraries. class CBuilder extends CTool implements Builder { /// The dart files involved in building this artifact. /// @@ -63,14 +82,8 @@ class CBuilder extends CTool implements Builder { /// sources: [ /// 'src/native_add_library.c', /// 'src/native_add_library.h', - /// 'src/dart_api_dl.c', - /// 'src/dart_api_dl.h', /// ], /// ``` - /// Supported by Clang-like compilers, which can optimize with precompiled - /// headers when `.h` files are included. If a compiler does not support this, - /// the build system may filter `.h` files from the compilation step while - /// still tracking them as dependencies. super.sources = const [], super.includes = const [], super.frameworks = CTool.defaultFrameworks, diff --git a/pkgs/native_toolchain_c/lib/src/cbuilder/ctool.dart b/pkgs/native_toolchain_c/lib/src/cbuilder/ctool.dart index 5d85127b3a..99230c4ce1 100644 --- a/pkgs/native_toolchain_c/lib/src/cbuilder/ctool.dart +++ b/pkgs/native_toolchain_c/lib/src/cbuilder/ctool.dart @@ -37,6 +37,13 @@ abstract class CTool { /// Resolved against [LinkInput.packageRoot]. /// /// The sources will be reported as dependencies of the hook. + /// + /// This should include both C source files (e.g., `.c`) and header files + /// (e.g., `.h`). Including header files ensures that changes to them + /// invalidate the build cache, triggering recompilation when necessary. + /// + /// If a compiler does not support this, the build system may filter `.h` files + /// from the compilation step while still tracking them as dependencies. final List sources; /// Include directories to pass to the linker. From f05846e45b18d483d394e94e1bc9bd9e31487951 Mon Sep 17 00:00:00 2001 From: MiniPiku <21parthib2006@gmail.com> Date: Thu, 13 Mar 2025 17:21:53 +0530 Subject: [PATCH 3/4] cbuilder comment modification --- .../lib/src/cbuilder/cbuilder.dart | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart b/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart index 8bb3083a28..a349f0213b 100644 --- a/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart +++ b/pkgs/native_toolchain_c/lib/src/cbuilder/cbuilder.dart @@ -72,18 +72,6 @@ class CBuilder extends CTool implements Builder { CBuilder.library({ required super.name, super.assetName, - /// The list of source files to build the library. - /// - /// This should include both C source files (e.g., `.c`) and header files - /// (e.g., `.h`). Including header files ensures that changes to them - /// invalidate the build cache, triggering recompilation when necessary. - /// For example, for a package named `native_add_library`: - /// ```dart - /// sources: [ - /// 'src/native_add_library.c', - /// 'src/native_add_library.h', - /// ], - /// ``` super.sources = const [], super.includes = const [], super.frameworks = CTool.defaultFrameworks, From 51daf5445a10053a3b52f92e625f02c645943c3a Mon Sep 17 00:00:00 2001 From: MiniPiku <21parthib2006@gmail.com> Date: Fri, 14 Mar 2025 23:21:28 +0530 Subject: [PATCH 4/4] fix : false change committment --- pkgs/native_assets_cli/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkgs/native_assets_cli/README.md b/pkgs/native_assets_cli/README.md index b7ab5c9921..687f038cd6 100644 --- a/pkgs/native_assets_cli/README.md +++ b/pkgs/native_assets_cli/README.md @@ -43,7 +43,7 @@ assets work on Dart stable, but prefer using the dev releases as we regularly break things. To use native assets in Flutter, use -`flutter config --enable-experiment=native-assets` and t +`flutter config --enable-experiment=native-assets` and then `flutter create --template=package_ffi `. In Flutter, the experiment is only available on the master channel.