Commit 88a08a6c authored by tburkard's avatar tburkard Committed by Commit bot

Add Go build support for GN.

Provide a skeleton of the mojo system API, as well as a skeleton test.
R=qsr@chromium.org, brettw@chromium.org, viettrungluu@chromium.org

Review URL: https://codereview.chromium.org/556813003

Cr-Commit-Position: refs/heads/master@{#297038}
parent 8bbaa87b
#!/usr/bin/env python
# Copyright (c) 2012 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""
This script invokes the go build tool.
Must be called as follows:
python go.py <go-binary> <build directory> <output file> <src directory>
<CGO_CFLAGS> <CGO_LDFLAGS> <go-binary options>
eg.
python go.py /usr/lib/google-golang/bin/go out/build out/a.out .. "-I."
"-L. -ltest" test -c test/test.go
"""
import argparse
import os
import shutil
import sys
def main():
parser = argparse.ArgumentParser()
parser.add_argument('go_binary')
parser.add_argument('build_directory')
parser.add_argument('output_file')
parser.add_argument('src_root')
parser.add_argument('cgo_cflags')
parser.add_argument('cgo_ldflags')
parser.add_argument('go_option', nargs='*')
args = parser.parse_args()
go_binary = args.go_binary
build_dir = args.build_directory
out_file = os.path.abspath(args.output_file)
# The src directory specified is relative. We need this as an absolute path.
src_root = os.path.abspath(args.src_root)
# GOPATH must be absolute, and point to one directory up from |src_Root|
go_path = os.path.abspath(os.path.join(src_root, ".."))
go_options = args.go_option
try:
shutil.rmtree(build_dir, True)
os.mkdir(build_dir)
except:
pass
old_directory = os.getcwd()
os.chdir(build_dir)
os.environ["GOPATH"] = go_path
os.environ["CGO_CFLAGS"] = args.cgo_cflags
os.environ["CGO_LDFLAGS"] = args.cgo_ldflags
os.system("%s %s" % (go_binary, " ".join(go_options)))
out_files = [ f for f in os.listdir(".") if os.path.isfile(f)]
if (len(out_files) > 0):
shutil.move(out_files[0], out_file)
os.chdir(old_directory)
try:
shutil.rmtree(build_dir, True)
except:
pass
if __name__ == '__main__':
sys.exit(main())
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
declare_args() {
# By default, there is no go build tool, because go builds are not supported.
go_build_tool = ""
}
# Declare a go test binary target.
#
# The target generates a go test executable, linking against other C code,
# which is compiled into a static library and linked against Go.
#
# Only works on linux. |go_build_tool| must be set to the absolute path
# of the go build tool.
#
# Variables (all required)
# sources: list of .go files to compile
# static_library_sources: list of C sources needed for the static library
# deps: dependencies for the static library
template("go_test_binary") {
# Only available on linux for now.
assert(is_linux)
assert(defined(invoker.sources))
assert(go_build_tool != "")
static_library_name = target_name + "_static_library"
static_library(static_library_name) {
sources = invoker.static_library_sources
deps = invoker.deps
complete_static_lib = true
}
action(target_name) {
deps = [
":$static_library_name",
]
script = "//build/go/go.py"
outputs = [ "${target_out_dir}/${target_name}" ]
# Since go test does not permit specifying an output directory or output
# binary name, we create a temporary build directory, and the python
# script will later identify the output, copy it to the target location,
# and clean up the temporary build directory.
build_dir = "${target_out_dir}/${target_name}_build"
args = [
"--",
"${go_build_tool}",
rebase_path(build_dir, root_build_dir),
rebase_path(target_out_dir, root_build_dir) + "/${target_name}",
rebase_path("//", root_build_dir),
"-I" + rebase_path("//"),
" -L" + rebase_path(target_out_dir) +
" -L" + rebase_path(root_build_dir + "/obj/third_party/libevent") +
" -l" + static_library_name +
" -lstdc++ -lpthread -lm -lglib-2.0 -levent",
"test", "-c",
] + rebase_path(invoker.sources, build_dir)
}
}
...@@ -7,6 +7,9 @@ import("//build/config/ui.gni") ...@@ -7,6 +7,9 @@ import("//build/config/ui.gni")
group("mojo") { group("mojo") {
# Meta-target, don't link into production code. # Meta-target, don't link into production code.
testonly = true testonly = true
declare_args() {
mojo_use_go = false
}
deps = [ deps = [
":tests", ":tests",
"//mojo/apps/js:mojo_js", "//mojo/apps/js:mojo_js",
...@@ -22,6 +25,11 @@ group("mojo") { ...@@ -22,6 +25,11 @@ group("mojo") {
deps += [ deps += [
"//mojo/python", "//mojo/python",
] ]
if (mojo_use_go) {
deps += [
"//mojo/go",
]
}
} }
} }
......
# Copyright 2014 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
import("//build/go/rules.gni")
group("go") {
deps = [
":system_test",
]
}
go_test_binary("system_test") {
sources = [
"tests/system_test.go",
]
static_library_sources = [
"c_embedder/c_embedder.cc",
"c_embedder/c_embedder.h",
]
deps = [
"//mojo/system",
]
}
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "mojo/go/c_embedder/c_embedder.h"
#include "base/memory/scoped_ptr.h"
#include "mojo/embedder/embedder.h"
#include "mojo/embedder/simple_platform_support.h"
#ifdef __cplusplus
extern "C" {
#endif
void InitializeMojoEmbedder() {
mojo::embedder::Init(scoped_ptr<mojo::embedder::PlatformSupport>(
new mojo::embedder::SimplePlatformSupport()));
}
#ifdef __cplusplus
} // extern "C"
#endif
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef MOJO_GO_C_EMBEDDER_C_EMBEDDER_H_
#define MOJO_GO_C_EMBEDDER_C_EMBEDDER_H_
#ifdef __cplusplus
extern "C" {
#endif
__attribute__((visibility("default"))) void InitializeMojoEmbedder();
#ifdef __cplusplus
} // extern "C"
#endif
#endif // MOJO_GO_C_EMBEDDER_C_EMBEDDER_H__
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package embedder;
/*
#include "mojo/go/c_embedder/c_embedder.h"
*/
import "C"
func InitializeMojoEmbedder() {
C.InitializeMojoEmbedder();
}
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package impl;
/*
#include "mojo/public/platform/native/system_thunks.h"
*/
import "C"
import "mojo/public/go/mojo/system"
type CoreImpl struct {
}
func (c CoreImpl) GetTimeTicksNow() int64 {
return (int64)(C.MojoGetTimeTicksNow());
}
var lazyInstance *CoreImpl = nil;
func GetCore() system.Core {
if lazyInstance == nil {
lazyInstance = new(CoreImpl);
}
return lazyInstance;
}
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package tests
import "mojo/go/system/embedder"
import "mojo/go/system/impl"
import "mojo/public/go/mojo/system"
import "testing"
func Init() {
embedder.InitializeMojoEmbedder();
}
func TestGetTimeTicksNow(t *testing.T) {
Init();
var c system.Core = impl.GetCore();
x := c.GetTimeTicksNow();
if (x < 10) {
t.Error("Invalid GetTimeTicksNow return value");
}
}
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package system;
type Core interface {
GetTimeTicksNow() int64
}
...@@ -2,6 +2,8 @@ ...@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be // Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file. // found in the LICENSE file.
// Note: This header should be compilable as C.
#ifndef MOJO_PUBLIC_PLATFORM_NATIVE_SYSTEM_THUNKS_H_ #ifndef MOJO_PUBLIC_PLATFORM_NATIVE_SYSTEM_THUNKS_H_
#define MOJO_PUBLIC_PLATFORM_NATIVE_SYSTEM_THUNKS_H_ #define MOJO_PUBLIC_PLATFORM_NATIVE_SYSTEM_THUNKS_H_
...@@ -43,9 +45,10 @@ struct MojoSystemThunks { ...@@ -43,9 +45,10 @@ struct MojoSystemThunks {
const MojoHandleSignals* signals, const MojoHandleSignals* signals,
uint32_t num_handles, uint32_t num_handles,
MojoDeadline deadline); MojoDeadline deadline);
MojoResult (*CreateMessagePipe)(const MojoCreateMessagePipeOptions* options, MojoResult (*CreateMessagePipe)(
MojoHandle* message_pipe_handle0, const struct MojoCreateMessagePipeOptions* options,
MojoHandle* message_pipe_handle1); MojoHandle* message_pipe_handle0,
MojoHandle* message_pipe_handle1);
MojoResult (*WriteMessage)(MojoHandle message_pipe_handle, MojoResult (*WriteMessage)(MojoHandle message_pipe_handle,
const void* bytes, const void* bytes,
uint32_t num_bytes, uint32_t num_bytes,
...@@ -58,7 +61,7 @@ struct MojoSystemThunks { ...@@ -58,7 +61,7 @@ struct MojoSystemThunks {
MojoHandle* handles, MojoHandle* handles,
uint32_t* num_handles, uint32_t* num_handles,
MojoReadMessageFlags flags); MojoReadMessageFlags flags);
MojoResult (*CreateDataPipe)(const MojoCreateDataPipeOptions* options, MojoResult (*CreateDataPipe)(const struct MojoCreateDataPipeOptions* options,
MojoHandle* data_pipe_producer_handle, MojoHandle* data_pipe_producer_handle,
MojoHandle* data_pipe_consumer_handle); MojoHandle* data_pipe_consumer_handle);
MojoResult (*WriteData)(MojoHandle data_pipe_producer_handle, MojoResult (*WriteData)(MojoHandle data_pipe_producer_handle,
...@@ -82,12 +85,12 @@ struct MojoSystemThunks { ...@@ -82,12 +85,12 @@ struct MojoSystemThunks {
MojoResult (*EndReadData)(MojoHandle data_pipe_consumer_handle, MojoResult (*EndReadData)(MojoHandle data_pipe_consumer_handle,
uint32_t num_elements_read); uint32_t num_elements_read);
MojoResult (*CreateSharedBuffer)( MojoResult (*CreateSharedBuffer)(
const MojoCreateSharedBufferOptions* options, const struct MojoCreateSharedBufferOptions* options,
uint64_t num_bytes, uint64_t num_bytes,
MojoHandle* shared_buffer_handle); MojoHandle* shared_buffer_handle);
MojoResult (*DuplicateBufferHandle)( MojoResult (*DuplicateBufferHandle)(
MojoHandle buffer_handle, MojoHandle buffer_handle,
const MojoDuplicateBufferHandleOptions* options, const struct MojoDuplicateBufferHandleOptions* options,
MojoHandle* new_buffer_handle); MojoHandle* new_buffer_handle);
MojoResult (*MapBuffer)(MojoHandle buffer_handle, MojoResult (*MapBuffer)(MojoHandle buffer_handle,
uint64_t offset, uint64_t offset,
...@@ -98,6 +101,8 @@ struct MojoSystemThunks { ...@@ -98,6 +101,8 @@ struct MojoSystemThunks {
}; };
#pragma pack(pop) #pragma pack(pop)
#ifdef __cplusplus
// Intended to be called from the embedder. Returns a |MojoCore| initialized // Intended to be called from the embedder. Returns a |MojoCore| initialized
// to contain pointers to each of the embedder's MojoCore functions. // to contain pointers to each of the embedder's MojoCore functions.
inline MojoSystemThunks MojoMakeSystemThunks() { inline MojoSystemThunks MojoMakeSystemThunks() {
...@@ -124,6 +129,8 @@ inline MojoSystemThunks MojoMakeSystemThunks() { ...@@ -124,6 +129,8 @@ inline MojoSystemThunks MojoMakeSystemThunks() {
}; };
return system_thunks; return system_thunks;
} }
#endif
// Use this type for the function found by dynamically discovering it in // Use this type for the function found by dynamically discovering it in
// a DSO linked with mojo_system. For example: // a DSO linked with mojo_system. For example:
...@@ -132,6 +139,7 @@ inline MojoSystemThunks MojoMakeSystemThunks() { ...@@ -132,6 +139,7 @@ inline MojoSystemThunks MojoMakeSystemThunks() {
// "MojoSetSystemThunks")); // "MojoSetSystemThunks"));
// The expected size of |system_thunks} is returned. // The expected size of |system_thunks} is returned.
// The contents of |system_thunks| are copied. // The contents of |system_thunks| are copied.
typedef size_t (*MojoSetSystemThunksFn)(const MojoSystemThunks* system_thunks); typedef size_t (*MojoSetSystemThunksFn)(
const struct MojoSystemThunks* system_thunks);
#endif // MOJO_PUBLIC_PLATFORM_NATIVE_SYSTEM_THUNKS_H_ #endif // MOJO_PUBLIC_PLATFORM_NATIVE_SYSTEM_THUNKS_H_
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment