xref: /aosp_15_r20/external/flatbuffers/samples/go_sample.sh (revision 890232f25432b36107d06881e0a25aaa6b473652)
1*890232f2SAndroid Build Coastguard Worker#!/bin/bash
2*890232f2SAndroid Build Coastguard Worker#
3*890232f2SAndroid Build Coastguard Worker# Copyright 2015 Google Inc. All rights reserved.
4*890232f2SAndroid Build Coastguard Worker#
5*890232f2SAndroid Build Coastguard Worker# Licensed under the Apache License, Version 2.0 (the "License");
6*890232f2SAndroid Build Coastguard Worker# you may not use this file except in compliance with the License.
7*890232f2SAndroid Build Coastguard Worker# You may obtain a copy of the License at
8*890232f2SAndroid Build Coastguard Worker#
9*890232f2SAndroid Build Coastguard Worker#     http://www.apache.org/licenses/LICENSE-2.0
10*890232f2SAndroid Build Coastguard Worker#
11*890232f2SAndroid Build Coastguard Worker# Unless required by applicable law or agreed to in writing, software
12*890232f2SAndroid Build Coastguard Worker# distributed under the License is distributed on an "AS IS" BASIS,
13*890232f2SAndroid Build Coastguard Worker# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14*890232f2SAndroid Build Coastguard Worker# See the License for the specific language governing permissions and
15*890232f2SAndroid Build Coastguard Worker# limitations under the License.
16*890232f2SAndroid Build Coastguard Worker#
17*890232f2SAndroid Build Coastguard Worker# Note: This script runs on Mac and Linux. It requires `go` to be installed
18*890232f2SAndroid Build Coastguard Worker# and 'flatc' to be built (using `cmake` in the root directory).
19*890232f2SAndroid Build Coastguard Worker
20*890232f2SAndroid Build Coastguard Workersampledir=$(cd $(dirname $BASH_SOURCE) && pwd)
21*890232f2SAndroid Build Coastguard Workerrootdir=$(cd $sampledir/.. && pwd)
22*890232f2SAndroid Build Coastguard Workercurrentdir=$(pwd)
23*890232f2SAndroid Build Coastguard Worker
24*890232f2SAndroid Build Coastguard Workerif [[ "$sampledir" != "$currentdir" ]]; then
25*890232f2SAndroid Build Coastguard Worker  echo Error: This script must be run from inside the $sampledir directory.
26*890232f2SAndroid Build Coastguard Worker  echo You executed it from the $currentdir directory.
27*890232f2SAndroid Build Coastguard Worker  exit 1
28*890232f2SAndroid Build Coastguard Workerfi
29*890232f2SAndroid Build Coastguard Worker
30*890232f2SAndroid Build Coastguard Worker# Run `flatc`. Note: This requires you to compile using `cmake` from the
31*890232f2SAndroid Build Coastguard Worker# root `/flatbuffers` directory.
32*890232f2SAndroid Build Coastguard Workerif [ -e ../flatc ]; then
33*890232f2SAndroid Build Coastguard Worker  ../flatc --go monster.fbs
34*890232f2SAndroid Build Coastguard Workerelif [ -e ../Debug/flatc ]; then
35*890232f2SAndroid Build Coastguard Worker  ../Debug/flatc --go monster.fbs
36*890232f2SAndroid Build Coastguard Workerelse
37*890232f2SAndroid Build Coastguard Worker  echo 'flatc' could not be found. Make sure to build FlatBuffers from the \
38*890232f2SAndroid Build Coastguard Worker       $rootdir directory.
39*890232f2SAndroid Build Coastguard Worker exit 1
40*890232f2SAndroid Build Coastguard Workerfi
41*890232f2SAndroid Build Coastguard Worker
42*890232f2SAndroid Build Coastguard Workerecho Compiling and running the Go sample.
43*890232f2SAndroid Build Coastguard Worker
44*890232f2SAndroid Build Coastguard Worker# Go requires a particular layout of files in order to link the necessary
45*890232f2SAndroid Build Coastguard Worker# packages. Copy these files to the respective directores to compile the
46*890232f2SAndroid Build Coastguard Worker# sample.
47*890232f2SAndroid Build Coastguard Workermkdir -p ${sampledir}/go_gen/src/MyGame/Sample
48*890232f2SAndroid Build Coastguard Workermkdir -p ${sampledir}/go_gen/src/github.com/google/flatbuffers/go
49*890232f2SAndroid Build Coastguard Workercp MyGame/Sample/*.go ${sampledir}/go_gen/src/MyGame/Sample/
50*890232f2SAndroid Build Coastguard Workercp ${sampledir}/../go/* ${sampledir}/go_gen/src/github.com/google/flatbuffers/go
51*890232f2SAndroid Build Coastguard Worker
52*890232f2SAndroid Build Coastguard Worker# Export the `GOPATH`, so that `go` will know which directories to search for
53*890232f2SAndroid Build Coastguard Worker# the libraries.
54*890232f2SAndroid Build Coastguard Workerexport GOPATH=${sampledir}/go_gen/
55*890232f2SAndroid Build Coastguard Worker
56*890232f2SAndroid Build Coastguard Worker# Compile and execute the sample.
57*890232f2SAndroid Build Coastguard Workergo build -o go_sample sample_binary.go
58*890232f2SAndroid Build Coastguard Worker./go_sample
59*890232f2SAndroid Build Coastguard Worker
60*890232f2SAndroid Build Coastguard Worker# Clean up the temporary files.
61*890232f2SAndroid Build Coastguard Workerrm -rf MyGame/
62*890232f2SAndroid Build Coastguard Workerrm -rf ${sampledir}/go_gen/
63*890232f2SAndroid Build Coastguard Workerrm go_sample
64