native icon indicating copy to clipboard operation
native copied to clipboard

[ffigen] Generate build.dart script if a .m file is generated

Open liamappelbe opened this issue 5 months ago • 2 comments

The main design goal is that the script should be composable. It should be easy for the user to integrate the new build stuff with their existing build script if they have one.

liamappelbe avatar Nov 20 '25 00:11 liamappelbe

@brianquinlan

brianquinlan avatar Nov 20 '25 23:11 brianquinlan

The main design goal is that the script should be composable.

I'd suggest something along the lines of:

hook/build.dart

import 'package:hooks/hooks.dart';
import 'package:sqlite/src/ffigen_build.g.dart';

void main(List<String> args) async {
  await build(args, (input, output) async {
    await buildFfiGenAsset(input, output);
  });
}

lib/src/ffigen_build.g.dart:

// GENERATED BY FFIGEN DO NOT EDIT.

import 'package:code_assets/code_assets.dart';
import 'package:hooks/hooks.dart';
import 'package:native_toolchain_c/native_toolchain_c.dart';

Future<void> buildFfiGenAsset(
  BuildInput input,
  BuildOutputBuilder output,
) async {
  if (input.config.buildCodeAssets) {
    final builder = CBuilder.library(
      // ...
    );
    await builder.run(input: input, output: output);
  }
}

And throw an error if there's no call await buildFfiGenAsset(input, output); in hook/build.dart but it does exist.

dcharkes avatar Nov 21 '25 09:11 dcharkes

I'll probably also generate a main() in ffigen_build.g.dart, so that if this is the user's only native asset they can just generate this file directly as their hook/build.dart.

And throw an error if there's no call await buildFfiGenAsset(input, output); in hook/build.dart but it does exist.

@dcharkes Not quite sure what you mean here. Isn't hook/build.dart written by the user in this hypothetical? How do we throw the error?

liamappelbe avatar Dec 02 '25 22:12 liamappelbe

I'd let the code generator generate the build hook if it doesn't exist yet, and if it already exists check that buildFfiGenAsset is called. And het the code generator always regenerate ffigen_build.g.dart.

So the build hook can be written by the user, in that case we check that it calls buildFfiGenAsset. But if it's not written by the user we just generate the hook.

dcharkes avatar Dec 04 '25 12:12 dcharkes