Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
eae3a7e
Created proof of concept CMake driven build of Welcome program.
DrErickson Aug 20, 2024
011bfe0
Moved filelib_isDirectory to before filelib_createDirectory fix scope…
DrErickson Aug 22, 2024
0d0818a
Moved filelib_isDirectory to before filelib_createDirectory fix scope…
DrErickson Aug 22, 2024
772c262
Created CMakeLists.txt for each "Project".
DrErickson Aug 22, 2024
46f9bbf
Fixed path issue with build.h generation
DrErickson Aug 23, 2024
a0babbd
Fixed header globbing
DrErickson Aug 23, 2024
bfe6d12
Use dll libraries by default.
DrErickson Aug 24, 2024
c3a5bfe
Fixed unit tests to include ALL test files.
DrErickson Aug 25, 2024
fa17089
Cleaned up find_package call
DrErickson Aug 30, 2024
e1f8aae
Fixed odd issue with gif icons. QPixmap does not support gif by defa…
DrErickson Aug 30, 2024
657d578
Moved CMAKE_PREFIX_PATH warning to library CMakeLists.txt
DrErickson Sep 2, 2024
05c625e
Library assumes resources are installed in a user writable data locat…
DrErickson Sep 2, 2024
51bbc77
Fixed user path issue for finding GenericDataLocation for mac and linux
DrErickson Sep 3, 2024
91b32ba
Fixed small issue with Welcome CMakeLists.txt (Welcome.cpp vs welcome…
SingaporeanViking Sep 3, 2024
c98ef3b
Minor cosmetic changes.
SingaporeanViking Sep 3, 2024
b92eb3e
Fixed path for msvc
DrErickson Sep 12, 2024
df4a052
Merge branch 'zelenski:master' into CMakeList-Build-Option
DrErickson Sep 13, 2024
db4fac5
Update CMakeLists.txt
DrErickson Sep 13, 2024
7b4fa15
Merge branch 'zelenski:master' into CMakeList-Build-Option
DrErickson Sep 21, 2024
a547ccb
Merge branch 'zelenski:master' into CMakeList-Build-Option
DrErickson Sep 25, 2024
7fc3840
Merge branch 'zelenski:master' into CMakeList-Build-Option
DrErickson Aug 27, 2025
5951138
Modified to automatically find the correct director of Qt install bas…
DrErickson Sep 17, 2025
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
158 changes: 158 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
cmake_minimum_required(VERSION 3.17)
project(stanford-cpp-library)

# This CMake provides an example of how to use CMake to build the
# Stanford cpp library rather than a pro file. This allows the use
# of other IDE's such as CLion, VSC, etc. for development.

# MinGW compiler lags, be conservative and use C++11 on all platforms
# rather than special case
set(CMAKE_CXX_STANDARD 11)

set(CS106_LIB_PATH "${CMAKE_CURRENT_SOURCE_DIR}/Library")

find_package(Qt6 COMPONENTS Core REQUIRED)

message(STATUS "Qt6 version: ${Qt6_VERSION}")

# We try to make a best guess based on the OS, but you may
# need to manually specify the path to the Qt library.
if (WIN32)

# find_qt_mingw64(QT_MINGW64_DIR)
# message("${QT_MINGW64_DIR}")
# MinGW is recommended for Windows. Please note that
# Qt is very picky about the MinGW compiler. Be sure
# you are using the most up to date version of MinGW.
set(CMAKE_PREFIX_PATH "C:/Qt/${Qt6_VERSION}/mingw_64")

# Alternatively MSVC 2019 compiler can be used (Not tested)
# set(CMAKE_PREFIX_PATH "C:/Qt/6.7.2/msvc2019_64")
elseif (APPLE)
# Note that execute_process cannot expand ~/ for the home path.
# We instead find the home directory using the environment variable
set(CMAKE_PREFIX_PATH "$ENV{HOME}/Qt/${Qt6_VERSION}/macos")
else ()
# UNIX/Linux case
# Same issue with expanding the home path
set(CMAKE_PREFIX_PATH "$ENV{HOME}/Qt/${Qt6_VERSION}/gcc_64")
endif ()

# Verify CMAKE_PREFIX_PATH exists
if (NOT EXISTS ${CMAKE_PREFIX_PATH})
message(WARNING "The path ${CMAKE_PREFIX_PATH} does not exist "
"please verify the path to your Qt installation")
endif ()

set(CMAKE_AUTOMOC ON) # Automatic handling of the Qt Meta-Object Compiler (MOC)
set(CMAKE_AUTORCC ON) # Automatic handling of the Qt Resource compiler
set(CMAKE_AUTOUIC ON) # Automatic handling of the Qt UI code generator

find_package(Qt6 REQUIRED COMPONENTS Core Gui Widgets Network Multimedia)

# Include all the cs106 library folders
include_directories("${CS106_LIB_PATH}")
include_directories("${CS106_LIB_PATH}/collections")
include_directories("${CS106_LIB_PATH}/console")
include_directories("${CS106_LIB_PATH}/graphics")
include_directories("${CS106_LIB_PATH}/io")
include_directories("${CS106_LIB_PATH}/private")
include_directories("${CS106_LIB_PATH}/resources")
include_directories("${CS106_LIB_PATH}/system")
include_directories("${CS106_LIB_PATH}/util")
include_directories("${CS106_LIB_PATH}/testing")

# Copy resources folder into per-user writable data location from QtStandardPaths
if (WIN32)
set(QTP_EXE "qtpaths6.exe")
else()
set(QTP_EXE "qtpaths6")
endif ()

set (QT_INSTALL_BINS "${CMAKE_PREFIX_PATH}/bin")

# Find writable data directory
execute_process(COMMAND "${QT_INSTALL_BINS}/${QTP_EXE}" "--writable-path" "GenericDataLocation"
OUTPUT_VARIABLE USER_DATA_DIR OUTPUT_STRIP_TRAILING_WHITESPACE)

# Copy resources folder into data directory
set (SPL_DIR "${USER_DATA_DIR}/cs106")
file(COPY "${CS106_LIB_PATH}/resources" DESTINATION "${SPL_DIR}")

# Used in build.h.in file to generate build.h file
set (SPL_VERSION "2023.1")
string(TIMESTAMP TODAY "%d/%m/%Y")
set (SPL_BUILD_DATE ${TODAY})

# Find user from environment variable
# Environment variable is either USERNAME or USER
if ("$ENV{USER}" STREQUAL "")
set (SPL_BUILD_USER $ENV{USERNAME})
else ()
set (SPL_BUILD_USER "$ENV{USER}")
endif ()

# Generate build.h file
add_compile_definitions(SPL_CMAKE_BUILD)
configure_file(
${CS106_LIB_PATH}/private/build.h.in
${CS106_LIB_PATH}/private/build.h
@ONLY
)

# Create list of source files
file(GLOB LIBRARY_SRC CONFIGURE_DEPENDS
"${CS106_LIB_PATH}/collections/*.cpp"
"${CS106_LIB_PATH}/console/*.cpp"
"${CS106_LIB_PATH}/graphics/*.cpp"
"${CS106_LIB_PATH}/io/*.cpp"
"${CS106_LIB_PATH}/system/*.cpp"
"${CS106_LIB_PATH}/util/*.cpp"
"${CS106_LIB_PATH}/testing/*.cpp"
"${CS106_LIB_PATH}/private/*.cpp"
)

# Create list of header files
file(GLOB LIBRARY_HEADER CONFIGURE_DEPENDS
"${CS106_LIB_PATH}/collections/*.h"
"${CS106_LIB_PATH}/console/*.h"
"${CS106_LIB_PATH}/graphics/*.h"
"${CS106_LIB_PATH}/io/*.h"
"${CS106_LIB_PATH}/system/*.h"
"${CS106_LIB_PATH}/util/*.h"
"${CS106_LIB_PATH}/testing/*.h"
"${CS106_LIB_PATH}/private/*.h"
)

file(GLOB QT_RESOURCES ${CS106_LIB_PATH}/images.qrc)

# We need to say what libraries we need from Qt6
# Qt is very picky about the compiler version.
# Make sure you check to make sure your compiler
# is up to date. You have been warned!
set(QT_VERSION 6)
set(REQUIRED_LIBS Core Gui Widgets Network Multimedia)
set(REQUIRED_LIBS_QUALIFIED Qt6::Core Qt6::Gui Qt6::Widgets Qt6::Network Qt6::Multimedia)

qt_add_resources(RCC_SOURCES ${QT_RESOURCES})

# https://doc.qt.io/qt-6/cmake-get-started.html
# Create static library
qt_add_library(CS106_library STATIC
${LIBRARY_SRC}
${LIBRARY_HEADER}
${RCC_SOURCES}
)

# Link CS106 library to required Qt libraries
target_link_libraries(CS106_library PRIVATE Qt6::Core Qt6::Gui Qt6::Widgets Qt6::Network Qt6::Multimedia)

# Call CMakeLists.txt files of subdirectories
add_subdirectory(Welcome)
add_subdirectory(SPL-unit-tests)
add_subdirectory(SimpleTestGuide)

# TODO: These need to be fixed/tested
# add_subdirectory(RandomClientTests/BugFixes)
# add_subdirectory(RandomClientTests/CompileFlags)
# add_subdirectory(RandomClientTests/ShadowTest)
40 changes: 19 additions & 21 deletions Library/images.qrc
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
<!DOCTYPE RCC><RCC version="1.0">
<qresource prefix="/">
<file alias="stanford">resources/splicon-large.png</file>

<file alias="about">resources/about.gif</file>
<file alias="background_color">resources/background_color.gif</file>
<file alias="clear_console">resources/clear_console.gif</file>
<file alias="compare_output">resources/compare_output.gif</file>
<file alias="copy">resources/copy.gif</file>
<file alias="cut">resources/cut.gif</file>
<file alias="font">resources/font.gif</file>
<file alias="load_input_script">resources/load_input_script.gif</file>
<file alias="paste">resources/paste.gif</file>
<file alias="print">resources/print.gif</file>
<file alias="quit">resources/quit.gif</file>
<file alias="save">resources/save.gif</file>
<file alias="save_as">resources/save_as.gif</file>
<file alias="select_all">resources/select_all.gif</file>
<file alias="text_color">resources/text_color.gif</file>
</qresource>

</RCC>
<qresource prefix="/">
<file alias="about">resources/about.png</file>
<file alias="background_color">resources/background_color.png</file>
<file alias="clear_console">resources/clear_console.png</file>
<file alias="compare_output">resources/compare_output.png</file>
<file alias="copy">resources/copy.png</file>
<file alias="cut">resources/cut.png</file>
<file alias="font">resources/font.png</file>
<file alias="load_input_script">resources/load_input_script.png</file>
<file alias="paste">resources/paste.png</file>
<file alias="print">resources/print.png</file>
<file alias="quit">resources/quit.png</file>
<file alias="save">resources/save.png</file>
<file alias="save_as">resources/save_as.png</file>
<file alias="select_all">resources/select_all.png</file>
<file alias="splicon-large">resources/splicon-large.png</file>
<file alias="text_color">resources/text_color.png</file>
</qresource>
</RCC>
14 changes: 11 additions & 3 deletions Library/private/build.h.in
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
#ifndef SPL_BUILD_H
#define SPL_BUILD_H

#define SPL_VERSION \"$$SPL_VERSION\"
#define SPL_BUILD_DATE \"$$_DATE_\"
#define SPL_BUILD_USER \"$$(USER)\"
#ifndef SPL_CMAKE_BUILD
// Building from pro file
#define SPL_VERSION \"$$SPL_VERSION\"
#define SPL_BUILD_DATE \"$$_DATE_\"
#define SPL_BUILD_USER \"$$(USER)\"
#else
// Building from CMake
#define SPL_VERSION "@SPL_VERSION@"
#define SPL_BUILD_DATE "@SPL_BUILD_DATE@"
#define SPL_BUILD_USER "@SPL_BUILD_USER@"
#endif

#endif
16 changes: 8 additions & 8 deletions Library/private/filelibunix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@
#include "strlib.h"

namespace platform {

bool filelib_isDirectory(const std::string& filename) {
struct stat fileInfo;
if (stat(filename.c_str(), &fileInfo) != 0) {
return false;
}
return S_ISDIR(fileInfo.st_mode) != 0;
}

void filelib_createDirectory(const std::string& path) {
std::string pathStr = path;
Expand Down Expand Up @@ -132,14 +140,6 @@ std::string filelib_getTempDirectory() {
return dir;
}

bool filelib_isDirectory(const std::string& filename) {
struct stat fileInfo;
if (stat(filename.c_str(), &fileInfo) != 0) {
return false;
}
return S_ISDIR(fileInfo.st_mode) != 0;
}

bool filelib_isFile(const std::string& filename) {
struct stat fileInfo;
if (stat(filename.c_str(), &fileInfo) != 0) {
Expand Down
10 changes: 5 additions & 5 deletions Library/private/filelibwindows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@

namespace platform {

bool filelib_isDirectory(const std::string& filename) {
DWORD attr = GetFileAttributesA(filename.c_str());
return attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_DIRECTORY);
}

void filelib_createDirectory(const std::string& path) {
std::string pathStr = path;
if (endsWith(path, "\\")) {
Expand Down Expand Up @@ -101,11 +106,6 @@ std::string filelib_getTempDirectory() {
return std::string(path, n);
}

bool filelib_isDirectory(const std::string& filename) {
DWORD attr = GetFileAttributesA(filename.c_str());
return attr != INVALID_FILE_ATTRIBUTES && (attr & FILE_ATTRIBUTE_DIRECTORY);
}

// https://msdn.microsoft.com/en-us/library/windows/desktop/gg258117(v=vs.85).aspx
bool filelib_isFile(const std::string& filename) {
DWORD attr = GetFileAttributesA(filename.c_str());
Expand Down
Binary file added Library/resources/about.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Library/resources/background_color.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Library/resources/clear_console.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Library/resources/compare_output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Library/resources/copy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Library/resources/cut.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Library/resources/font.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Library/resources/load_input_script.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
File renamed without changes
Binary file added Library/resources/paste.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Library/resources/print.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Library/resources/quit.png
Binary file added Library/resources/save.png
Binary file added Library/resources/save_as.png
Binary file added Library/resources/select_all.png
Binary file added Library/resources/text_color.png
53 changes: 53 additions & 0 deletions RandomClientTests/BugFixes/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
cmake_minimum_required(VERSION 3.17)
project(BugFixes)

# Need the relative path to Library
set(CS106_LIB_PATH "../../Library")

# Create the project executable
add_executable(${PROJECT_NAME}
bugfixes.cpp
)

# link exe to CS106 library
target_link_libraries(${PROJECT_NAME} CS106_library)

# student writes ordinary main() function, but it must be called within a
# wrapper main() that handles library setup/teardown. Rename student's
# to distinguish between the two main() functions and avoid symbol clash
# Ask Julie if you are curious why main->qMain->studentMain
target_compile_definitions(${PROJECT_NAME} PRIVATE main=qMain)
target_compile_definitions(${PROJECT_NAME} PRIVATE qMain=studentMain)

find_package(Qt${QT_VERSION} COMPONENTS ${REQUIRED_LIBS} REQUIRED)
target_link_libraries(${PROJECT_NAME} ${REQUIRED_LIBS_QUALIFIED})

if (WIN32)
if (CMAKE_BUILD_TYPE MATCHES "Debug")
set(EXTENSION "debug")
else ()
set(EXTENSION "dll")
endif ()
set(QT_INSTALL_PATH "${CMAKE_PREFIX_PATH}")
if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
endif ()
endif ()
if (EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows.dll")
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory
"$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
"${QT_INSTALL_PATH}/plugins/platforms/qwindows.${EXTENSION}"
"$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
endif ()
foreach (QT_LIB ${REQUIRED_LIBS})
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
"${QT_INSTALL_PATH}/bin/Qt${QT_VERSION}${QT_LIB}.${EXTENSION}"
"$<TARGET_FILE_DIR:${PROJECT_NAME}>")
endforeach (QT_LIB)
endif ()
53 changes: 53 additions & 0 deletions RandomClientTests/CompileFlags/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
cmake_minimum_required(VERSION 3.17)
project(CompileFlags)

# Need the relative path to Library
set(CS106_LIB_PATH "../../Library")

# Create the project executable
add_executable(${PROJECT_NAME}
compile.cpp
)

# link exe to CS106 library
target_link_libraries(${PROJECT_NAME} CS106_library)

# student writes ordinary main() function, but it must be called within a
# wrapper main() that handles library setup/teardown. Rename student's
# to distinguish between the two main() functions and avoid symbol clash
# Ask Julie if you are curious why main->qMain->studentMain
target_compile_definitions(${PROJECT_NAME} PRIVATE main=qMain)
target_compile_definitions(${PROJECT_NAME} PRIVATE qMain=studentMain)

find_package(Qt${QT_VERSION} COMPONENTS ${REQUIRED_LIBS} REQUIRED)
target_link_libraries(${PROJECT_NAME} ${REQUIRED_LIBS_QUALIFIED})

if (WIN32)
if (CMAKE_BUILD_TYPE MATCHES "Debug")
set(EXTENSION "debug")
else ()
set(EXTENSION "dll")
endif ()
set(QT_INSTALL_PATH "${CMAKE_PREFIX_PATH}")
if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
if (NOT EXISTS "${QT_INSTALL_PATH}/bin")
set(QT_INSTALL_PATH "${QT_INSTALL_PATH}/..")
endif ()
endif ()
if (EXISTS "${QT_INSTALL_PATH}/plugins/platforms/qwindows.dll")
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E make_directory
"$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
"${QT_INSTALL_PATH}/plugins/platforms/qwindows.${EXTENSION}"
"$<TARGET_FILE_DIR:${PROJECT_NAME}>/plugins/platforms/")
endif ()
foreach (QT_LIB ${REQUIRED_LIBS})
add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy
"${QT_INSTALL_PATH}/bin/Qt${QT_VERSION}${QT_LIB}.${EXTENSION}"
"$<TARGET_FILE_DIR:${PROJECT_NAME}>")
endforeach (QT_LIB)
endif ()
Loading