How to build a .mojom target with ninja

1,689 views
Skip to first unread message

Chacha Marwa

unread,
Sep 17, 2020, 4:45:58 PM9/17/20
to chromium-mojo
Hi,

Noob here  i was learning to use mojom interface and got stuck for four days now with this error here.

ninja: error: unknown target '/home/user_name/chromium/src/example/public/mojom'

i was following this documentation here (https://chromium.googlesource.com/chromium/src.git/+/master/docs/mojo_and_services.md), and am sure that i am missing out a certain step while following up the illustrations on the page.

what i did:
1. ~/chromium/src$ mkdir -p example/public/mojom
2. ~/chromium/src/example/public/mojom$ touch ping_responder.mojom && nano ping_responder.mojom

pasted this interface data in  ping_responder.mojom

module example.mojom;

interface PingResponder {
  // Receives a "Ping" and responds with a random integer.
  Ping() => (int32 random);
};


3. ~/chromium/src/example/public/mojom$ touch BUILD.gn && nano BUILD.gn

pasted this details in BUILD.gn

import("//mojo/public/tools/bindings/mojom.gni")
mojom("mojom") {
  sources = [ "ping_responder.mojom" ]
}

4. ~/chromium/src/example/public/mojom$ gn gen out/r
i did this because of step 5 threw an error of not finding the out/r directory while excuting the command on step 5 (am not sure if this i right)

5.~/chromium/src/example/public/mojom$ ninja -C out/r ~/chromium/src/example/public/mojom

hope one you guys can spot my mistake there. also on top of that i would like to ask these questions here

1. is it right to create a BUILD.gn manualy like how i did on step 3 there.
(i asked because i was thinking that BUILD.gn is suposed
 to be automatically genreated from a certain command that i don't know like (ie. gn gen args.gn)
2. i get comfused by the ending line of this ninja statement while building js bingings and c++ bindings because the former (js) adds a colon and the other doesn't.

for c++ 
ninja -C out/r ~/chromium/src/example/public/mojom

for js
ninja -C out/r services/echo/public/interfaces:interfaces_js

NB; just stabed this group while searching for solution am not as well sure if its a right place to post this, in any case if this is not an apropriate place for this i present my apologies in advance in respect to ever one annoyed by my post thanks.



Joe Mason

unread,
Sep 17, 2020, 5:12:22 PM9/17/20
to Chacha Marwa, chromium-mojo
GN starts at one top-level target and then searches all its dependencies for other targets to build. So even though you added an //example/public/mojom target, GN can't find it unless some other target references it.

As a quick test, try adding "//example/public/mojom" to the gn_all deps in chromium/src/BUILD.gn:

group("gn_all") {
testonly = true
deps = [
":gn_visibility",
"//base:base_perftests",
(etc)

(There's a comment at the top of that BUILD.gn file explaining how this works a bit more.)

deps = [ "//example/public/mojom" ]

To 

--
You received this message because you are subscribed to the Google Groups "chromium-mojo" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-moj...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-mojo/1ba33897-7eaf-48b6-b196-fb54047f0db2n%40chromium.org.

Chacha Marwa

unread,
Sep 18, 2020, 1:40:34 AM9/18/20
to chromium-mojo, Joe Mason, chromium-mojo, Chacha Marwa
Thanks for the reply Joe, that is what i was missing.

but after i added the that the generation proces started and threw this exception 

[1/1] Regenerating ninja files
[5/7] ACTION //services/echo/public/interfaces:interfaces_js__generator(//build/toolchain/linux:clang_x64)
FAILED: gen/services/echo/public/interfaces/echo.mojom.js gen/services/echo/public/interfaces/echo.mojom.externs.js gen/services/echo/public/interfaces/echo.mojom-lite.js gen/services/echo/public/interfaces/echo.mojom.html gen/services/echo
/public/interfaces/echo.mojom-lite-for-compile.js  
python ../../../../../../mojo/public/tools/bindings/mojom_bindings_generator.py --use_bundled_pylibs -o gen generate -d ../../../../../../ -I ../../../../../../ --bytecode_path gen/mojo/public/tools/bindings --filelist=__services_echo_publi
c_interfaces_interfaces_js__generator___build_toolchain_linux_clang_x64__rule..rsp -g javascript --js_bindings_mode=new --scrambled_message_id_salt_path ../../../../../../chrome/VERSION
Traceback (most recent call last):
 File "../../../../../../mojo/public/tools/bindings/mojom_bindings_generator.py", line 390, in <module>
   sys.exit(main())
 File "../../../../../../mojo/public/tools/bindings/mojom_bindings_generator.py", line 385, in main
   return args.func(args, remaining_args)
 File "../../../../../../mojo/public/tools/bindings/mojom_bindings_generator.py", line 250, in _Generate
   RelativePath(filename, args.depth, args.output_dir), [])
 File "../../../../../../mojo/public/tools/bindings/mojom_bindings_generator.py", line 95, in __init__
   raise Exception("Invalid input path %s" % path)
Exception: Invalid input path ../../echo.mojom
ninja: build stopped: subcommand failed.

what am i missing there?

Chacha Marwa

unread,
Sep 19, 2020, 4:10:14 AM9/19/20
to chromium-mojo, Chacha Marwa, Joe Mason, chromium-mojo
OKey so  imanaged to solve the error above after reading the bindings generator script mojo_bindings_generator.py
it turns out that i was running ninja inside a wrong directory
like this.

~/chromium/src/example/public/mojom$ ninja -C out/r example/public/mojom

there fore ninja couldn't determine a relative path from the directory where i was (current working directory) this one (example/public/mojom)

so i had to switch/go back to the root dir  "src/" and GN a new output dir from there and run ninja from there and everthing went well .

Thanks Joe Mason for helping me out with the first issue.

Joe Mason

unread,
Sep 21, 2020, 11:25:03 AM9/21/20
to Chacha Marwa, chromium-mojo
Glad you got it working!

Bharani Tharan

unread,
May 25, 2023, 1:26:52 PM5/25/23
to chromium-mojo, Joe Mason, chromium-mojo, Chacha Marwa
I'm also facing the ninja: error: unknown target '/home/user_name/repo/chromium/src/chrome/services/math/public/mojom'

After I have added below code in BUILD.gn in src folder
+++++++++++++++++++
group("gn_all") {
testonly = true
deps = [
":gn_visibility",
"//base:base_perftests",
+++++++++++++++++++
Then Build the particular service(math) using ninja -C out/r ~/chromium/src/example/public/mojom . This case I have observed below error.

Error:
ninja: Entering directory `out/r'
[0/1] Regenerating ninja files
ERROR Unresolved dependencies.
//:gn_all(//build/toolchain/linux:clang_x64)
  needs //:gn_visibility(//build/toolchain/linux:clang_x64)

FAILED: build.ninja.stamp
../../../../../../../buildtools/linux64/gn --root=../../../../../../.. -q --regeneration gen .
ninja: error: rebuilding 'build.ninja': subcommand failed

Kindly support me to solve the error. Also kindly share the example source code for better understanding about client server communication for browser and render process.
Reply all
Reply to author
Forward
0 new messages