Skip to content

Commit

Permalink
Add an option to build with system tree-sitter (#16679)
Browse files Browse the repository at this point in the history
As of tree-sitter/tree-sitter#602 it is possible
to install tree-sitter as a system shared library, and distributions that want to
be able to disable vendored code.
  • Loading branch information
eli-schwartz committed Apr 23, 2020
1 parent db0aa2d commit 03bc3c5
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 45 deletions.
2 changes: 1 addition & 1 deletion meson.build
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
project('radare2', 'c', license: 'LGPL3', meson_version: '>=0.47.0')
project('radare2', 'c', license: 'LGPL3', meson_version: '>=0.50.1')

py3_exe = import('python').find_installation('python3')
git_exe = find_program('git', required: false)
Expand Down
1 change: 1 addition & 0 deletions meson_options.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ option('use_sys_zlib', type: 'boolean', value: false)
option('use_sys_lz4', type: 'boolean', value: false)
option('use_sys_xxhash', type: 'boolean', value: false)
option('use_sys_openssl', type: 'boolean', value: false)
option('use_sys_tree_sitter', type: 'boolean', value: false)
option('use_libuv', type: 'boolean', value: true)
option('debugger', type: 'boolean', value: true)

Expand Down
95 changes: 51 additions & 44 deletions shlr/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -233,56 +233,62 @@ sdb_gen_cmd = [
]

# handle tree-sitter
tree_sitter_path = join_paths(meson.current_source_dir(), 'tree-sitter')
tree_sitter_vc_path = join_paths(meson.current_source_dir(), 'tree-sitter.vc')
if get_option('tree-sitter-sync')
if not git_exe.found()
error('Cannot sync tree-sitter library. Either provide tree-sitter in ./shlr/tree-sitter or install git, so it can be downloaded')
endif
tree_sitter_dep = dependency('tree-sitter', required: false)
if not tree_sitter_dep.found() or not get_option('use_sys_tree_sitter')
message('Use bundled tree-sitter')
tree_sitter_path = join_paths(meson.current_source_dir(), 'tree-sitter')
tree_sitter_vc_path = join_paths(meson.current_source_dir(), 'tree-sitter.vc')
if get_option('tree-sitter-sync')
if not git_exe.found()
error('Cannot sync tree-sitter library. Either provide tree-sitter in ./shlr/tree-sitter or install git, so it can be downloaded')
endif

# NOTE: when you update TS_TIP or TS_BRA, also update them in shlr/Makefile
TS_TIP = 'f049ba350f3f6019ce9a1cbb0975ebd154ef7ad3'
TS_BRA = 'master'

message('Deleting existing directories @0@ and @1@'.format(tree_sitter_vc_path, tree_sitter_path))
res = run_command('rm', '-rf @0@ @1@'.format(tree_sitter_vc_path, tree_sitter_path).split())
message('Cloning tree-sitter ' + TS_BRA + ' branch, commit ' + TS_TIP + ', into ' + tree_sitter_vc_path)
git_cmd = 'clone -b @0@ https://github.com/tree-sitter/tree-sitter.git @1@'.format(TS_BRA, tree_sitter_vc_path)
clone_cmd = run_command(git_exe, git_cmd.split())
if clone_cmd.returncode() != 0
error('Cannot execute git clone command')
endif
# NOTE: when you update TS_TIP or TS_BRA, also update them in shlr/Makefile
TS_TIP = 'f049ba350f3f6019ce9a1cbb0975ebd154ef7ad3'
TS_BRA = 'master'

reset_cmd_str = '-C @0@ reset --hard @1@'.format(tree_sitter_vc_path, TS_TIP)
reset_cmd = run_command(git_exe, reset_cmd_str.split())
if reset_cmd.returncode() != 0
error('Cannot execute git reset command')
endif
message('Deleting existing directories @0@ and @1@'.format(tree_sitter_vc_path, tree_sitter_path))
res = run_command('rm', '-rf @0@ @1@'.format(tree_sitter_vc_path, tree_sitter_path).split())
message('Cloning tree-sitter ' + TS_BRA + ' branch, commit ' + TS_TIP + ', into ' + tree_sitter_vc_path)
git_cmd = 'clone -b @0@ https://github.com/tree-sitter/tree-sitter.git @1@'.format(TS_BRA, tree_sitter_vc_path)
clone_cmd = run_command(git_exe, git_cmd.split())
if clone_cmd.returncode() != 0
error('Cannot execute git clone command')
endif

message('Copying files from @0@ to @1@'.format(tree_sitter_vc_path, tree_sitter_path))
res = run_command('mkdir', '-p @0@/lib'.format(tree_sitter_path).split())
res = run_command('cp', '-r @0@/lib/src @1@/lib'.format(tree_sitter_vc_path, tree_sitter_path).split())
res = run_command('cp', '-r @0@/lib/include @1@/lib'.format(tree_sitter_vc_path, tree_sitter_path).split())
message('Deleting @0@'.format(tree_sitter_vc_path))
res = run_command('rm', '-rf @0@'.format(tree_sitter_vc_path).split())
endif
reset_cmd_str = '-C @0@ reset --hard @1@'.format(tree_sitter_vc_path, TS_TIP)
reset_cmd = run_command(git_exe, reset_cmd_str.split())
if reset_cmd.returncode() != 0
error('Cannot execute git reset command')
endif

tree_sitter_files = [
join_paths(tree_sitter_path, 'lib/src/lib.c'),
]
message('Copying files from @0@ to @1@'.format(tree_sitter_vc_path, tree_sitter_path))
res = run_command('mkdir', '-p @0@/lib'.format(tree_sitter_path).split())
res = run_command('cp', '-r @0@/lib/src @1@/lib'.format(tree_sitter_vc_path, tree_sitter_path).split())
res = run_command('cp', '-r @0@/lib/include @1@/lib'.format(tree_sitter_vc_path, tree_sitter_path).split())
message('Deleting @0@'.format(tree_sitter_vc_path))
res = run_command('rm', '-rf @0@'.format(tree_sitter_vc_path).split())
endif

tree_sitter_inc = [platform_inc, include_directories('tree-sitter/lib/src'), include_directories('tree-sitter/lib/include')]
tree_sitter_files = [
join_paths(tree_sitter_path, 'lib/src/lib.c'),
]

libtree_sitter = static_library('tree_sitter', tree_sitter_files,
include_directories: tree_sitter_inc,
implicit_include_directories: false,
c_args: ['-std=c99']
)
tree_sitter_inc = [platform_inc, include_directories('tree-sitter/lib/src'), include_directories('tree-sitter/lib/include')]

tree_sitter_dep = declare_dependency(
link_with: libtree_sitter,
include_directories: tree_sitter_inc
)
libtree_sitter = static_library('tree_sitter', tree_sitter_files,
include_directories: tree_sitter_inc,
implicit_include_directories: false,
c_args: ['-std=c99']
)

tree_sitter_dep = declare_dependency(
link_with: libtree_sitter,
include_directories: tree_sitter_inc
)
else
message('Use system-provided tree-sitter library')
endif


# new radare2 shell parser
Expand All @@ -295,7 +301,8 @@ shell_parser_files = [
shell_parser_inc = [platform_inc, include_directories('radare2-shell-parser/src/tree_sitter')]

libshell_parser = static_library('shell_parser', shell_parser_files,
include_directories: shell_parser_inc + tree_sitter_inc,
include_directories: shell_parser_inc,
dependencies: tree_sitter_dep.partial_dependency(includes: true),
implicit_include_directories: true
)

Expand Down

0 comments on commit 03bc3c5

Please sign in to comment.