• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Copyright JS Foundation and other contributors, http://js.foundation
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14
15cmake_minimum_required (VERSION 2.8.12)
16project (unit-doc C)
17find_package(PythonInterp REQUIRED)
18
19set(GEN_DOCTEST "${CMAKE_SOURCE_DIR}/tools/gen-doctest.py")
20file(GLOB DOC_FILES "${CMAKE_SOURCE_DIR}/docs/*.md")
21
22if(NOT (${CMAKE_C_COMPILER_ID} STREQUAL MSVC))
23  set(COMPILE_FLAGS_DOCTEST "-Wno-unused-parameter -Wno-unused-function -Wno-unused-variable")
24endif()
25
26
27# Dry run of the doctest generator: analyze MarkDown files and get the list of
28# file names that will be generated. This allows the definition of proper
29# dependencies between the MarkDown files and the generated sources.
30execute_process(
31  COMMAND ${PYTHON_EXECUTABLE} ${GEN_DOCTEST} --dry -d ${CMAKE_CURRENT_BINARY_DIR} ${DOC_FILES}
32  OUTPUT_VARIABLE DOCTEST_OUTPUT
33  RESULT_VARIABLE GEN_DOCTEST_RESULT
34)
35if(NOT GEN_DOCTEST_RESULT EQUAL 0)
36  message(FATAL_ERROR "failed to get doctest file list")
37endif()
38
39# Process the output of the doctest generator: collect sources that must be
40# compiled, compiled+linked, or compiled+linked+executed into separate lists.
41set(DOCTEST_COMPILE "")
42set(DOCTEST_LINK "")
43set(DOCTEST_RUN "")
44
45string(REPLACE "\n" ";" DOCTEST_LIST "${DOCTEST_OUTPUT}")
46foreach(DOCTEST_ELEMENT IN LISTS DOCTEST_LIST)
47  if(NOT ("${DOCTEST_ELEMENT}" STREQUAL ""))
48    separate_arguments(DOCTEST_ELEMENT)
49    list(GET DOCTEST_ELEMENT 0 DOCTEST_ACTION)
50    list(GET DOCTEST_ELEMENT 1 DOCTEST_NAME)
51    string(TOUPPER ${DOCTEST_ACTION} DOCTEST_ACTION)
52
53    set(DOCTEST_${DOCTEST_ACTION} ${DOCTEST_${DOCTEST_ACTION}} ${DOCTEST_NAME})
54  endif()
55endforeach()
56
57# Disable 02.API-REFERENCE-create-context.c on Windows, because there is no pthread on it.
58if("${PLATFORM}" STREQUAL "WINDOWS")
59  list(REMOVE_ITEM DOCTEST_COMPILE ${CMAKE_CURRENT_BINARY_DIR}/02.API-REFERENCE-create-context.c)
60endif()
61
62# Disable 11.EXT-REFERENCE-AUTORELEASE.c if compiler is MSVC, because MSVC doesn't support cleanup attribute.
63if(USING_MSVC)
64  list(REMOVE_ITEM DOCTEST_COMPILE ${CMAKE_CURRENT_BINARY_DIR}/11.EXT-REFERENCE-AUTORELEASE.c)
65endif()
66
67# Add custom command to run doctest generator if any of the MarkDown sources
68# changes.
69add_custom_command(
70  COMMAND ${GEN_DOCTEST} -d ${CMAKE_CURRENT_BINARY_DIR} ${DOC_FILES}
71  DEPENDS ${GEN_DOCTEST} ${DOC_FILES}
72  OUTPUT ${DOCTEST_COMPILE} ${DOCTEST_LINK} ${DOCTEST_RUN}
73  COMMENT "Generating doctests"
74)
75
76# Add custom target to trigger the custom command above. Targets below can/must
77# depend on this one so that the custom command gets executed only once.
78add_custom_target(all-doc-files DEPENDS ${DOCTEST_COMPILE} ${DOCTEST_LINK} ${DOCTEST_RUN})
79
80# Process compile-only doctests: add them to a dummy library
81# (named libcompile-doc-tests.a) to trigger compilation.
82if(NOT ("${DOCTEST_COMPILE}" STREQUAL ""))
83  add_library(compile-doc-tests ${DOCTEST_COMPILE})
84  add_dependencies(compile-doc-tests all-doc-files)
85  target_link_libraries(compile-doc-tests jerry-ext jerry-core jerry-port-default-minimal)
86  set_property(TARGET compile-doc-tests APPEND_STRING PROPERTY COMPILE_FLAGS "${COMPILE_FLAGS_DOCTEST}")
87endif()
88
89macro(doctest_add_executables NAME_PREFIX)
90  foreach(DOCTEST_NAME ${ARGN})
91    get_filename_component(TARGET_NAME ${DOCTEST_NAME} NAME)
92    string(REGEX REPLACE "\\.[^.]*$" "" TARGET_NAME ${TARGET_NAME})
93    set(TARGET_NAME ${NAME_PREFIX}-${TARGET_NAME})
94
95    add_executable(${TARGET_NAME} ${DOCTEST_NAME})
96    add_dependencies(${TARGET_NAME} all-doc-files)
97    set_property(TARGET ${TARGET_NAME} APPEND_STRING PROPERTY COMPILE_FLAGS "${COMPILE_FLAGS_DOCTEST}")
98    set_property(TARGET ${TARGET_NAME} PROPERTY LINK_FLAGS "${LINKER_FLAGS_COMMON}")
99    set_property(TARGET ${TARGET_NAME} PROPERTY RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests")
100    target_link_libraries(${TARGET_NAME} jerry-ext jerry-core jerry-port-default-minimal)
101  endforeach()
102endmacro()
103
104# Process link-only doctests: create an executable from each (named link-doc-*)
105doctest_add_executables("link-doc" ${DOCTEST_LINK})
106
107# Process "full-fledged" doctests: create an executable from each (named
108# unit-doc-*). Their name prefix (unit-) ensures that the unit test runner
109# script will treat them like any other unit test, i.e., executed them.
110doctest_add_executables("unit-doc" ${DOCTEST_RUN})
111