Skip to content
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
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 41 additions & 12 deletions json2cmake/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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))

Expand All @@ -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:
Copy link
Owner

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.

relative_to_build = os.path.relpath(file, source_directory)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/source_directory/build_directory/

if not relative_to_build.startswith(os.path.pardir):
file = '${CMAKE_BINARY_DIR}/' + relative_to_build
output.write(' %s\n' % file)
output.write(')\n')

Expand All @@ -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')

Expand Down Expand Up @@ -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__':
Expand Down