naqvis / webview

Crystal bindings to Webview library
MIT License
94 stars 8 forks source link

change for linux: compilation #1

Closed AS400JPLPC closed 3 years ago

AS400JPLPC commented 3 years ago

I made changes to the file to fork AS400JPLPC

Makefile for Linux

UNAME := $(shell uname)

ifeq ($(UNAME), Darwin)
CFLAGS = -DWEBVIEW_COCOA=1 -DOBJC_OLD_DISPATCH_PROTOTYPES=1
endif

ifeq ($(UNAME), Linux)
CFLAGS = -DWEBVIEW_GTK=1 `pkg-config --cflags --libs gtk+-3.0 webkit2gtk-4.0`
endif

CFLAGS += -std=c++17  
cpp_file := ext/webview.cc
obj_file := $(cpp_file:.cc=.o)

.PHONY: all
all: $(obj_file)

ifeq ($(UNAME), Linux)
    ar rcs ext/libwebview.a ext/webview.o
endif

%.o: %.cc
    $(CXX) -c -o $@ $(CFLAGS) $<

.PHONY: clean
clean:
            rm -f $(obj_file)

src / lib.cr

module Webview
   # formule invalide for Linux 
   #@[Link(ldflags: "-L#{__DIR__}/../ext -lwebview.o -lc++")]

  {% if flag?(:darwin) %}
    # ?? tester ou prendre celui de linux 
    @[Link(ldflags: "-L#{__DIR__}/../ext -lwebview.o -lc++")]
    @[Link(framework: "WebKit")]
  {% elsif flag?(:linux) %}
    #------------------------------- tester ok -lc++ invalide  flag -L .../ext -lwebview.o invalide
    @[Link(ldflags: "#{__DIR__}/../ext/libwebview.a -lstdc++")]
    @[Link(ldflags: "`command -v pkg-config > /dev/null && pkg-config --cflags --libs gtk+-3.0 webkit2gtk-4.0`")]

  {% elsif flag?(:windows) %}
    # Windows requires special linker flags for GUI apps.
    @[Link(ldflags: "-L#{__DIR__}/../ext -lwebview.o -lc++")]
    @[Link(ldflags: "-lole32 -lcomctl32 -loleaut32 -luuid -lgdi32 -H windowsgui")]
  {% else %}
    raise "Platform not supported"
  {% end %}
  lib LibWebView
    alias T = Void*

    # Creates a new webview instance. If debug is non-zero - developer tools will
    # be enabled (if the platform supports them). Window parameter can be a
    # pointer to the native window handle. If it's non-null - then child WebView
    # is embedded into the given parent window. Otherwise a new window is created.
    # Depending on the platform, a GtkWindow, NSWindow or HWND pointer can be
    # passed here.
    fun create = webview_create(debug : LibC::Int, window : Void*) : T
    # Destroys a webview and closes the native window.
    fun destroy = webview_destroy(w : T)
    # Runs the main loop until it's terminated. After this function exits - you
    # must destroy the webview.
    fun run = webview_run(w : T)
    # Stops the main loop. It is safe to call this function from another other
    # background thread.
    fun terminate = webview_terminate(w : T)
    # Posts a function to be executed on the main thread. You normally do not need
    # to call this function, unless you want to tweak the native window.
    fun dispatch = webview_dispatch(w : T, fn : (T, Void* -> Void), arg : Void*)
    # Returns a native window handle pointer. When using GTK backend the pointer
    # is GtkWindow pointer, when using Cocoa backend the pointer is NSWindow
    # pointer, when using Win32 backend the pointer is HWND pointer.
    fun get_window = webview_get_window(w : T) : Void*
    # Updates the title of the native window. Must be called from the UI thread.
    fun set_title = webview_set_title(w : T, title : LibC::Char*)
    # Updates native window size. See WEBVIEW_HINT constants.
    fun set_size = webview_set_size(w : T, width : LibC::Int, height : LibC::Int, hints : LibC::Int)
    # Navigates webview to the given URL. URL may be a data URI, i.e.
    # "data:text/text,<html>...</html>". It is often ok not to url-encode it
    # properly, webview will re-encode it for you.
    fun navigate = webview_navigate(w : T, url : LibC::Char*)

    # Injects JavaScript code at the initialization of the new page. Every time
    # the webview will open a the new page - this initialization code will be
    # executed. It is guaranteed that code is executed before window.onload.
    fun init = webview_init(w : T, js : LibC::Char*)
    # Evaluates arbitrary JavaScript code. Evaluation happens asynchronously, also
    # the result of the expression is ignored. Use RPC bindings if you want to
    # receive notifications about the results of the evaluation.
    fun eval = webview_eval(w : T, js : LibC::Char*)
    # Binds a native C callback so that it will appear under the given name as a
    # global JavaScript function. Internally it uses webview_init(). Callback
    # receives a request string and a user-provided argument pointer. Request
    # string is a JSON array of all the arguments passed to the JavaScript
    # function.
    fun bind = webview_bind(w : T, name : LibC::Char*, fn : (LibC::Char*, LibC::Char*, Void* -> Void), arg : Void*)
    # Allows to return a value from the native binding. Original request pointer
    # must be provided to help internal RPC engine match requests with responses.
    # If status is zero - result is expected to be a valid JSON result value.
    # If status is not zero - result is an error JSON object.
    fun webview_return(w : T, seq : LibC::Char*, status : LibC::Int, result : LibC::Char*)
  end
end

Makfile and src/lib.cr I requested modifications to the original for me it's OK what is not tested is for mac

naqvis commented 3 years ago

Thanks @AS400JPLPC, can you please explain the reason for change? how it differs from existing one? or how existing one wasn't suitable enough for your use case?

AS400JPLPC commented 3 years ago

Thanks @AS400JPLPC, can you please explain the reason for change? how it differs from existing one? or how existing one wasn't suitable enough for your use case?

mahefile it is necessary to say where it finds the source and the lib to make the links ifeq ($(UNAME), Linux) CFLAGS = -DWEBVIEW_GTK=1 pkg-config --cflags --libs gtk+-3.0 webkit2gtk-4.0 endif

if you want to make a link -L you have to give it a lib ifeq ($(UNAME), Linux) ar rcs ext/libwebview.a ext/webview.o endif


libs.cr

on linux we don't know -lc ++ on linux an object is not a lib -lwebview.o

{% elsif flag?(:linux) %} @[Link(ldflags: "#{DIR}/../ext/libwebview.a -lstdc++")] @[Link(ldflags: "`command -v pkg-config > /d

@bientôt

naqvis commented 3 years ago

it is necessary to say where it finds the source and the lib to make the links

And existing version isn't doing that? Aren't you able to use this shard on your linux box? I've developed this shard on Mac and tested on Linux and Windows.

Also the linking libraries is done in lib.cr via @[Link] annotation instead of make file. Makefile is solely used to compile the c source to object file and that object file is in turn linked to crystal compiled binary.

It should be better if you can provide the problem you were experiencing, which made you to this solution. Then we can discuss the solution pros and cons.

AS400JPLPC commented 3 years ago

[soleil@HPZ800 ~]$ cd /home/soleil/cryweb/ [soleil@HPZ800 cryweb]$ shards install Resolving dependencies Fetching https://github.com/naqvis/webview.git Installing webview (0.1.1) Postinstall of webview: make Failed postinstall of webview on make: g++ -c -o ext/webview.o -DWEBVIEW_GTK=1 -std=c++11 ext/webview.cc Dans le fichier inclus depuis ext/webview.cc:2: ext/webview.h:418:10: erreur fatale: JavaScriptCore/JavaScript.h : Aucun fichier ou dossier de ce type 418 | #include <JavaScriptCore/JavaScript.h> | ^~~~~~~~~ compilation terminée. make: *** [Makefile:19 : ext/webview.o] Erreur 1 [soleil@HPZ800 cryweb]$

/usr/include/webkitgtk-4.0/JavaScriptCore /usr/lib/libjavascriptcoregtk-4.0.so /usr/lib/libjavascriptcoregtk-4.0.so.18 /usr/lib/libjavascriptcoregtk-4.0.so.18.17.11 /usr/lib/girepository-1.0/JavaScriptCore-4.0.typelib /usr/lib/pkgconfig/javascriptcoregtk-4.0.pc /usr/share/gir-1.0/JavaScriptCore-4.0.gir

/usr/include/webkitgtk-4.0/JavaScriptCore/JavaScript.h /usr/include/webkitgtk-4.0/JavaScriptCore/JSBase.h /usr/include/webkitgtk-4.0/JavaScriptCore/JSContextRef.h /usr/include/webkitgtk-4.0/JavaScriptCore/JSObjectRef.h /usr/include/webkitgtk-4.0/JavaScriptCore/JSStringRef.h /usr/include/webkitgtk-4.0/JavaScriptCore/JSTypedArray.h /usr/include/webkitgtk-4.0/JavaScriptCore/JSValueRef.h /usr/include/webkitgtk-4.0/JavaScriptCore/WebKitAvailability.h

naqvis commented 3 years ago

Thank you @AS400JPLPC . I've make your proposed changes and tested it on Linux.