-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add --source-dir and --build-dir flags #33
Open
arichardson
wants to merge
1
commit into
AbigailBuccaneer:master
Choose a base branch
from
arichardson:build-dir
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -75,13 +75,24 @@ def parsecommand(command, resolvepath): | |
} | ||
|
||
|
||
def get_include_path(include_path, source_dir): | ||
result = os.path.relpath(include_path, source_dir) | ||
def get_include_path(include_path, source_dir, build_dir): | ||
# Check if include path is below build directory and if so add ${CMAKE_BINARY_DIR}/: | ||
if build_dir and build_dir != source_dir: | ||
relative_to_build = os.path.relpath(include_path, build_dir) | ||
if relative_to_build == '.': | ||
return '${CMAKE_BINARY_DIR}' | ||
if not relative_to_build.startswith(os.path.pardir): | ||
return '${CMAKE_BINARY_DIR}/' + relative_to_build | ||
if source_dir is None: | ||
return include_path | ||
relative_to_source = os.path.relpath(include_path, source_dir) | ||
# For paths that are not below the source root write the absolute path | ||
# to the CMakeLists instead of ../../../usr/local/include | ||
if os.path.isabs(include_path) and result.startswith(os.path.pardir): | ||
if os.path.isabs(include_path) and relative_to_source.startswith(os.path.pardir): | ||
return include_path | ||
return result | ||
if relative_to_source == '.': | ||
return '${CMAKE_SOURCE_DIR}' | ||
return '${CMAKE_SOURCE_DIR}/' + relative_to_source | ||
|
||
|
||
class CompilationDatabase(object): | ||
|
@@ -110,7 +121,7 @@ def resolve(path): | |
file = resolve(entry['file']) | ||
self.targets.setdefault(command, set()).add(file) | ||
|
||
def write(self, output, directory=None, name='autogenerated'): | ||
def write(self, output, source_directory=None, build_directory=None, name='autogenerated'): | ||
output.write('cmake_minimum_required(VERSION 2.8.8)\n') | ||
output.write('project({})\n\n'.format(name)) | ||
|
||
|
@@ -134,6 +145,14 @@ def write(self, output, directory=None, name='autogenerated'): | |
|
||
output.write('add_library(%s OBJECT\n' % name) | ||
for file in files: | ||
if source_directory: | ||
relative_to_source = os.path.relpath(file, source_directory) | ||
if not relative_to_source.startswith(os.path.pardir): | ||
file = '${CMAKE_SOURCE_DIR}/' + relative_to_source | ||
elif build_directory: | ||
relative_to_build = os.path.relpath(file, source_directory) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
if not relative_to_build.startswith(os.path.pardir): | ||
file = '${CMAKE_BINARY_DIR}/' + relative_to_build | ||
output.write(' %s\n' % file) | ||
output.write(')\n') | ||
|
||
|
@@ -151,25 +170,22 @@ def write(self, output, directory=None, name='autogenerated'): | |
|
||
output.write('target_include_directories(%s PRIVATE\n' % name) | ||
for include in config['includes']: | ||
if directory is not None: | ||
include = get_include_path(include, directory) | ||
include = get_include_path(include, source_directory, build_directory) | ||
output.write(' %s\n' % include) | ||
output.write(')\n') | ||
|
||
if config.get('system_includes'): | ||
output.write( | ||
'target_include_directories(%s SYSTEM PRIVATE\n' % name) | ||
for include in config['system_includes']: | ||
if directory is not None: | ||
include = get_include_path(include, directory) | ||
include = get_include_path(include, source_directory, build_directory) | ||
output.write(' %s\n' % include) | ||
output.write(')\n\n') | ||
if config.get('iquote_includes'): | ||
output.write( | ||
'target_include_directories(%s BEFORE PRIVATE\n' % name) | ||
for include in config['iquote_includes']: | ||
if directory is not None: | ||
include = get_include_path(include, directory) | ||
include = get_include_path(include, source_directory, build_directory) | ||
output.write(' %s\n' % include) | ||
output.write(')\n\n') | ||
|
||
|
@@ -214,14 +230,27 @@ def main(): | |
name of the CMake project (default: taken from Git root, or 'autogenerated') | ||
""" | ||
) | ||
parser.add_argument( | ||
'--build-dir', help=""" | ||
path to the build directory (for projects building outside of the source directory). | ||
Paths starting with this prefix will use CMAKE_BINARY_DIR in the generated CMakeLists.txt | ||
""" | ||
) | ||
parser.add_argument( | ||
'--source-dir', help=""" | ||
path to the build directory (for projects building outside of the source dir). | ||
Paths starting with this prefix will use CMAKE_SOURCE_DIR in the generated CMakeLists.txt | ||
""" | ||
) | ||
args = parser.parse_args() | ||
|
||
if args.name is None: | ||
args.name = get_default_name(args.infile) | ||
|
||
database = CompilationDatabase() | ||
database.read(args.infile) | ||
database.write(args.outfile, name=args.name) | ||
database.write(args.outfile, name=args.name, source_directory=args.source_dir, | ||
build_directory=args.build_dir) | ||
|
||
|
||
if __name__ == '__main__': | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This branch will never be taken if
source_directory and build_directory
, but I think it should.