Skip to content

How to automatically copy external dynamic library or framework to build folder #296

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
befovy opened this issue Nov 2, 2019 · 13 comments
Closed
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers hover

Comments

@befovy
Copy link
Contributor

befovy commented Nov 2, 2019

Flutter version

[✓] Flutter (Channel beta, v1.10.7, on Mac OS X 10.14.6 18G95, locale zh-Hans-CN)

Plugin version

I'm developing a new plugin.

Go.mod file

plugin go.mod

module github.com/befovy/fijkplayer/go

go 1.13

require github.com/go-flutter-desktop/go-flutter v0.30.0

example go.mod

module fijkplayer_example/go

go 1.13

require (
	github.com/befovy/fijkplayer/go v0.1.9
	github.com/go-flutter-desktop/go-flutter v0.30.0
	github.com/go-flutter-desktop/plugins/path_provider v0.3.1
	github.com/go-flutter-desktop/plugins/shared_preferences v0.4.1
	github.com/pkg/errors v0.8.1
)

replace github.com/befovy/fijkplayer/go v0.1.9 => ../../go

Steps to Reproduce

I used cgo in my plugin

// #cgo CFLAGS: -I ${SRCDIR}/darwin/IJKPlayer.framework/Headers
// #cgo LDFLAGS: -F ${SRCDIR}/darwin -framework IJKPlayer
// #include "IJKFFCMediaPlayer.h"
import "C"

cgo link a framework

It can compile. But framework can not be found when run

› hover run
hover: Using engine from cache
hover: Cleaning the build directory
hover: Bundling flutter app
hover: Compiling 'go-flutter' and plugins
hover: Successfully compiled
hover: Build finished, starting app...
hover: Running fijkplayer_example in debug mode
dyld: Library not loaded: @rpath/IJKPlayer.framework/Versions/A/IJKPlayer
  Referenced from: /Users/befovy/Flutter/fijkplayer/example/./go/build/outputs/darwin/fijkplayer_example
  Reason: image not found
hover: App 'fijkplayer_example' exited with error: signal: abort trap

If I copy IJKPlayer.framework to /Users/befovy/Flutter/fijkplayer/example/./go/build/outputs/darwin and
cd /Users/befovy/Flutter/fijkplayer/example/./go/build/outputs/darwin, double click the binary file fijkplayer_example , it run successfully.

Every time I run hover run, IJKPlayer.framework which I have copied to /Users/befovy/Flutter/fijkplayer/example/./go/build/outputs/darwin will be deleted.
Is there a way to copy files to the build output folder atomically ? Thanks

@pchampio
Copy link
Member

pchampio commented Nov 3, 2019

It's kinda the same issue as #203

Since we are using the golang tool-chain to generate apps (hover, at it's core, is wrapping go build and flutter build bundle), we don't have access to build automation tool (i.e.: makefile, gradle,...).

It's is currently impossible to have a plugin copy dlib in the outputs/OS/ directory. (As a big workaround for plugin dev you can use: hover run --omit-flutter which wont delete the outputs/OS/ dir)

@pchampio pchampio added enhancement New feature or request hover labels Nov 3, 2019
@pchampio
Copy link
Member

pchampio commented Nov 3, 2019

What we can have is a dlib/{linux, darwin, windows} folder in the plugin dir (i.e.: https://github.com/befovy/fijkplayer/tree/master/go/dlib/linux)
Every file in those directory would be copied into outputs/$OS by hover plugins get.

That is the best I can thinks of.

@jld3103 what do you think?

@pchampio
Copy link
Member

pchampio commented Nov 3, 2019

BTW, awesome the see someone building a plugin!
make sure to read https://github.com/go-flutter-desktop/plugins/tree/master/video_player 😉

@provokateurin
Copy link
Member

@pchampio I think that would be the best solution. But the problem is, that it looks like the file needs to be copied into the .framework folder. Maybe something like hover assets which maps the source files to a location in the build folder is needed. (Also specific for every OS)

@pchampio
Copy link
Member

pchampio commented Nov 3, 2019

But the problem is, that it looks like the file needs to be copied into the .framework folder.

Humm, I don't think it works like that.
@befovy Does the output wanted is something like this?:

$ pwd
~/lab/flutter/go-flutter-example/draggable_borderless/go/build/outputs/darwin
$ ls
assets  draggable_borderless  flutter_assets  FlutterEmbedder.framework   IJKPlayer.framework   icudtl.dat

Maybe something like hover assets which maps the source files to a location in the build folder is needed.

Exactly, but for plugin, the dlib will need to be copied from ~/.pub-cache/hosted/pub.dartlang.org/$PLUGIN_NAME/go/dlib/linux/*.so to go/build/outputs/linux and so on for each platform!

@befovy
Copy link
Contributor Author

befovy commented Nov 4, 2019

Thanks the answer from all of you.

$pwd
~/fijkplayer/example/go/build/outputs/darwin
$ ls
FlutterEmbedder.framework fijkplayer_example icudtl.dat
assets flutter_assets IJKPlayer.framework

Yes, I want output like above. @pchampio

And, I also thinks a dlib/{linux, darwin, windows} folder in the plugin dir is a good solution.

But it seems it dose not work.
I create dlib/darwin both in plugin root dir (https://github.com/befovy/fijkplayer/tree/master) and plugin/go dir (https://github.com/befovy/fijkplayer/tree/master/go), and touch files in the created folder. After run hover plugins get in example dir, nothing have been copied to example/go/build/outputs.

BTW, I'm using rpath ldflags // #cgo LDFLAGS: -F ${SRCDIR}/darwin -framework IJKPlayer "-Wl,-rpath,${SRCDIR}/darwin" before a usable solution.

Is "rpath ldfalgs" a good way when I finished the plugin and want to publish it?

@pchampio
Copy link
Member

pchampio commented Nov 4, 2019

The implementation hasn't been coded yet^^

And rpath overwrite shouldn't be necessary once we have the auto copy of dlib

@pchampio
Copy link
Member

pchampio commented Nov 6, 2019

If someone wants to work on this, here is how this feature should be implemented:

  • When running hover plugins get under this line copies the content of the dep.pluginGoSource / "dlibs" folder (if exist) to go/build/intermediates of the project.
  • After the Cleaning the build directory procedure under here. Copies the content of go/build/intermediates/$targetOS to go/build/outputs/$targetOS with $targetOS the targetOS func argument
  • Propose some documentation text to be added to this wiki page
  • To avoid the need for // #cgo LDFLAGS: -F ${SRCDIR}/darwin add the -F or -L, go/build/intermediates path here. This way, only // #cgo LDFLAGS: -framework IJKPlayer is required.
    (-Wl,-rpath on darwin is already used in go-flutter/embedder/build.go)

Edit: added 4th point

@pchampio pchampio added the good first issue Good for newcomers label Nov 6, 2019
@befovy
Copy link
Contributor Author

befovy commented Nov 12, 2019

Please assign this issue to me. I will make a pull request in this week.

@befovy
Copy link
Contributor Author

befovy commented Nov 13, 2019

@pchampio
What should do if two different go-flutter plugins have file with the same name in there dlib folder ?

@provokateurin
Copy link
Member

Maybe prefix the file with the plugin name (do it always for consistency)

@pchampio
Copy link
Member

pchampio commented Nov 13, 2019

I don't thinks we can rename share libs without having to rename the CGO import (i.e.: // #cgo darwin LDFLAGS: -framework XXX).
If there is another solution, I'll take it.

PS: I'm not sure how renaming libs affect import.

@pchampio
Copy link
Member

Fixed by go-flutter-desktop/hover#59
Tagged in [email protected]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers hover
Development

No branches or pull requests

3 participants