Skip to content

Commit

Permalink
Merge pull request #59 from haosulab/code_refactor
Browse files Browse the repository at this point in the history
Code refactor 1st pass
  • Loading branch information
KolinGuo authored Jan 22, 2024
2 parents 4ad2106 + fb054f4 commit a6cd762
Show file tree
Hide file tree
Showing 40 changed files with 439 additions and 334 deletions.
29 changes: 13 additions & 16 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,45 @@ cmake_minimum_required(VERSION 3.12)
project(mp LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 17)

set(CMAKE_CXX_FLAGS_RELEASE "-O3 -g3 -Wall -fsized-deallocation -Wno-deprecated-declarations")
set(CMAKE_CXX_FLAGS_RELEASE "-O3 -g3 -Wall -Werror -fsized-deallocation -Wno-deprecated-declarations")
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O3")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")

# set -fuse-ld=lld if lld is found
find_program(LLD_FOUND ld.lld)
if(LLD_FOUND)
message(STATUS "Using lld")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld")
message(STATUS "Using lld")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -fuse-ld=lld")
endif(LLD_FOUND)

# add ccache as compiler launcher
find_program(CCACHE_FOUND ccache)
if(CCACHE_FOUND)
message(STATUS "Using ccache")
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
message(STATUS "Using ccache")
set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
endif(CCACHE_FOUND)

# Pinocchio uses its own FindCppAD, but does not provide it.
set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/CMakeModules")

set(Boost_NO_WARN_NEW_VERSIONS 1) # silence Boost CMake warnings
find_package(Eigen3 3.4.0 REQUIRED)
find_package(Boost COMPONENTS system filesystem REQUIRED)
set(Boost_NO_WARN_NEW_VERSIONS 1) # silence Boost CMake warnings
find_package(Boost REQUIRED COMPONENTS system filesystem)
find_package(ompl REQUIRED)
find_package(fcl REQUIRED)
find_package(pinocchio REQUIRED)
find_package(assimp REQUIRED)
find_package(orocos_kdl REQUIRED)
find_package(urdfdom REQUIRED)

include_directories(${OMPL_INCLUDE_DIRS} ${urdfdom_INCLUDE_DIRS})
include_directories("src")

# store libries in a variable
set(LIBS ompl fcl assimp orocos-kdl Boost::system Boost::filesystem urdfdom_model urdfdom_world)

# pymp
file(GLOB_RECURSE PROJECT_SRC "src/*.h" "src/*.cpp" "src/*.hpp")
add_library(mp STATIC ${PROJECT_SRC})
# mp
file(GLOB_RECURSE MPLIB_SRC "src/*.cpp")
add_library(mp STATIC ${MPLIB_SRC})
target_link_libraries(mp PRIVATE ${LIBS})
target_include_directories(mp PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>)
target_include_directories(mp PUBLIC ${OMPL_INCLUDE_DIRS} ${urdfdom_INCLUDE_DIRS})
set_target_properties(mp PROPERTIES POSITION_INDEPENDENT_CODE TRUE)

# pybind11_mkdoc
Expand All @@ -51,15 +49,14 @@ add_custom_target(
COMMAND bash "${CMAKE_CURRENT_SOURCE_DIR}/dev/mkdoc.sh"
"-I$<JOIN:$<TARGET_PROPERTY:mp,INCLUDE_DIRECTORIES>,;-I>"
BYPRODUCTS "${CMAKE_CURRENT_SOURCE_DIR}/python/docstring/*.h"
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/dev/mkdoc.sh" "${CMAKE_CURRENT_SOURCE_DIR}/src/*.h"
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/dev/mkdoc.sh" "${CMAKE_CURRENT_SOURCE_DIR}/include/mplib/*.h"
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
COMMAND_EXPAND_LISTS
VERBATIM
)

# Pybind11
add_subdirectory("third_party/pybind11")
include_directories("python")
pybind11_add_module(pymp python/pybind.cpp)
target_link_libraries(pymp PRIVATE mp)
add_dependencies(pymp pybind11_mkdoc)
Expand Down
2 changes: 1 addition & 1 deletion dev/mkdoc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# Additional flags that clang understands can be passed in as well
CLANG_FLAGS="-std=c++17 ${@}"
PY_SCRIPT_PATH="dev/mkdoc.py"
CPP_INCLUDE_DIR="src"
CPP_INCLUDE_DIR="include/mplib"
OUTPUT_DOCSTRING_DIR="python/docstring"

############################################################
Expand Down
47 changes: 24 additions & 23 deletions dev/test_mkdoc/drake/libclang_setup.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import platform
import os
import platform
import subprocess

from clang import cindex


# Alternative: Make this a function in `mkdoc.py`, and import it from mkdoc as
# a module? (if this was really authored in `mkdoc.py`...)

Expand All @@ -19,35 +18,37 @@ def add_library_paths(parameters=None):
Returns:
"""
library_file = None
if platform.system() == 'Darwin':
completed_process = subprocess.run(['xcrun', '--find', 'clang'],
stdout=subprocess.PIPE,
encoding='utf-8')
if platform.system() == "Darwin":
completed_process = subprocess.run(
["xcrun", "--find", "clang"], stdout=subprocess.PIPE, encoding="utf-8"
)
if completed_process.returncode == 0:
toolchain_dir = os.path.dirname(os.path.dirname(
completed_process.stdout.strip()))
library_file = os.path.join(
toolchain_dir, 'lib', 'libclang.dylib')
completed_process = subprocess.run(['xcrun', '--show-sdk-path'],
stdout=subprocess.PIPE,
encoding='utf-8')
toolchain_dir = os.path.dirname(
os.path.dirname(completed_process.stdout.strip())
)
library_file = os.path.join(toolchain_dir, "lib", "libclang.dylib")
completed_process = subprocess.run(
["xcrun", "--show-sdk-path"], stdout=subprocess.PIPE, encoding="utf-8"
)
if parameters is not None and completed_process.returncode == 0:
sdkroot = completed_process.stdout.strip()
if os.path.exists(sdkroot):
parameters.append('-isysroot')
parameters.append("-isysroot")
parameters.append(sdkroot)
elif platform.system() == 'Linux':
elif platform.system() == "Linux":
# By default we expect Clang 14 to be installed, but on Ubuntu 20.04
# we'll use Clang 12 (because Clang 14 isn't packaged).
version = 14
completed_process = subprocess.run(['lsb_release', '-sr'],
stdout=subprocess.PIPE,
encoding='utf-8')
if completed_process.returncode == 0:
if completed_process.stdout.strip() == '20.04':
version = 12
completed_process = subprocess.run(
["lsb_release", "-sr"], stdout=subprocess.PIPE, encoding="utf-8"
)
if (
completed_process.returncode == 0
and completed_process.stdout.strip() == "20.04"
):
version = 12
arch = platform.machine()
library_file = f'/usr/lib/{arch}-linux-gnu/libclang-{version}.so'
library_file = f"/usr/lib/{arch}-linux-gnu/libclang-{version}.so"
if not os.path.exists(library_file):
raise RuntimeError(f'Library file {library_file} does NOT exist')
raise RuntimeError(f"Library file {library_file} does NOT exist")
cindex.Config.set_library_file(library_file)
91 changes: 44 additions & 47 deletions dev/test_mkdoc/drake/mkdoc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-
#
# Derived from https://github.com/pybind/pybind11/
#
# Copyright (c) 2016 Wenzel Jakob <[email protected]>,
Expand Down Expand Up @@ -267,9 +265,9 @@ def extract_comment(cursor, deprecations):

# Append the deprecation text.
result += (
r" (Deprecated.) \deprecated {} "
"This will be removed from Drake on or after {}."
).format(message, removal_date)
rf" (Deprecated.) \deprecated {message} "
f"This will be removed from Drake on or after {removal_date}."
)

return result

Expand Down Expand Up @@ -442,7 +440,7 @@ def specialize_well_known_doc_var_names():
# overload naming set.
result[i] = None
continue
elif any([symbols[i].comment == x.comment for x in symbols[:i]]):
elif any(symbols[i].comment == x.comment for x in symbols[:i]):
# If a subsequent overload's API comment *exactly* matches a
# prior overload's comment, the first overload's name wins.
# This is important because when a function has separate
Expand Down Expand Up @@ -518,7 +516,7 @@ def specialize_well_known_doc_var_names():
]

# The argument count might be sufficient to disambiguate.
result = ["doc_{}args".format(len(types)) for types in overload_arg_types]
result = [f"doc_{len(types)}args" for types in overload_arg_types]
specialize_well_known_doc_var_names()
if is_unique(result):
return result
Expand Down Expand Up @@ -581,11 +579,11 @@ def iprint(s):
name_var = sanitize_name(name_var)
# We may get empty symbols if `libclang` produces warnings.
assert len(name_var) > 0, node.first_symbol.sorting_key()
iprint("// Symbol: {}".format(full_name))
iprint(f"// Symbol: {full_name}")
modifier = ""
if level == 0:
modifier = "constexpr "
iprint("{}struct /* {} */ {{".format(modifier, name_var))
iprint(f"{modifier}struct /* {name_var} */ {{")

# Print documentation items.
symbol_iter = sorted(node.doc_symbols, key=Symbol.sorting_key)
Expand All @@ -599,10 +597,8 @@ def iprint(s):
delim = "\n"
if "\n" not in comment and len(comment) < 40:
delim = " "
iprint(" // Source: {}:{}".format(symbol.include, symbol.line))
iprint(
' const char* {} ={}R"""({})""";'.format(doc_var, delim, comment.strip())
)
iprint(f" // Source: {symbol.include}:{symbol.line}")
iprint(f' const char* {doc_var} ={delim}R"""({comment.strip()})""";')

# Recurse into child elements.
keys = sorted(node.children_map.keys())
Expand All @@ -624,21 +620,23 @@ def iprint(s):
if (node.children_map[x].first_symbol.cursor.kind == CursorKind.FIELD_DECL)
]
if field_names:
iprint(f" auto Serialize__fields() const {{")
iprint(f" return std::array{{")
iprint(" auto Serialize__fields() const {")
iprint(" return std::array{{")
for x in field_names:
iprint(f' std::make_pair("{x}", {x}.doc),')
iprint(f" }};")
iprint(f" }}")
iprint("}} {};".format(name_var))
iprint(" }};")
iprint(" }}")
iprint(f"}} {name_var};")


class FileDict:
"""
Provides a dictionary that hashes based on a file's true path.
"""

def __init__(self, items=[]):
def __init__(self, items=None):
if items is None:
items = []
self._d = {self._key(file): value for file, value in items}

def _key(self, file):
Expand Down Expand Up @@ -694,14 +692,14 @@ def main():
eprint("Syntax: %s -output=<file> [.. a list of header files ..]" % sys.argv[0])
sys.exit(1)

f = open(output_filename, "w", encoding="utf-8")
f = open(output_filename, "w", encoding="utf-8") # noqa: SIM115

# N.B. We substitute the `GENERATED FILE...` bits in this fashion because
# otherwise Reviewable gets confused.
f.write(
"""#pragma once
// {0} {1}
// {} {}
// This file contains docstrings for the Python bindings that were
// automatically extracted by mkdoc.py.
Expand All @@ -719,14 +717,11 @@ def main():
# Determine project include directories.
# N.B. For simplicity when using with Bazel, we do not try to get canonical
# file paths for determining include files.
include_paths = []
for param in parameters:
# Only check for normal include directories.
if param.startswith("-I"):
include_paths.append(param[2:])
include_paths = [param[2:] for param in parameters if param.startswith("-I")]

# Use longest include directories first to get shortest include file
# overall.
include_paths = list(sorted(include_paths, key=len))[::-1]
include_paths = sorted(include_paths, key=len)[::-1]
include_files = []
# Create mapping from filename to include file.
include_file_map = FileDict()
Expand All @@ -739,7 +734,7 @@ def main():
break
else:
raise RuntimeError(
"Filename not incorporated into -I includes: {}".format(filename)
f"Filename not incorporated into -I includes: {filename}"
)
for p in ignore_patterns:
if fnmatch(include_file, p):
Expand All @@ -765,7 +760,7 @@ def main():
glue_f.write("#include <optional>\n")
# Add the includes to the glue, and as comments in the output.
for include_file in sorted(include_files):
line = '#include "{}"'.format(include_file)
line = f'#include "{include_file}"'
glue_f.write(line + "\n")
f.write("// " + line + "\n")
f.write("\n")
Expand All @@ -782,21 +777,23 @@ def main():
raise RuntimeError("Parsing headers using the clang library failed")
# If there is an error on line 1, that means the C++ standard library
# include paths are broken.
if translation_unit.diagnostics:
if translation_unit.diagnostics[0].location.line == 1:
try:
# Use '###' to dump Clang's include paths to stdout.
index.parse("foo", parameters + ["-###"])
except Exception:
pass
raise RuntimeError(
"The operating system's C++ standard library is not "
"installed correctly or is only partially installed. For "
"example, libgcc-??-dev is installed but libstdc++-??-dev "
"is not installed (the ?? indicates a version number). "
"Try re-running Drake's install_prereqs, or maybe check "
"your system and install anything that's missing by hand."
)
if (
translation_unit.diagnostics
and translation_unit.diagnostics[0].location.line == 1
):
try:
# Use '###' to dump Clang's include paths to stdout.
index.parse("foo", parameters + ["-###"])
except Exception:
pass
raise RuntimeError(
"The operating system's C++ standard library is not "
"installed correctly or is only partially installed. For "
"example, libgcc-??-dev is installed but libstdc++-??-dev "
"is not installed (the ?? indicates a version number). "
"Try re-running Drake's install_prereqs, or maybe check "
"your system and install anything that's missing by hand."
)
severities = [
diagnostic.severity
for diagnostic in translation_unit.diagnostics
Expand Down Expand Up @@ -826,12 +823,12 @@ def main():
except UnicodeEncodeError as e:
# User-friendly error for #9903.
print(
"""
Encountered unicode error: {}
f"""
Encountered unicode error: {e}
If you are on Ubuntu, please ensure you have en_US.UTF-8 locales generated:
sudo apt-get install --no-install-recommends locales
sudo locale-gen en_US.UTF-8
""".format(e),
""",
file=sys.stderr,
)
sys.exit(1)
Expand Down
Loading

0 comments on commit a6cd762

Please sign in to comment.